Hi, I'm just starting out with C++, although I've been programming PHP for ages (so i know functions, looping, etc), and a little bit of C, but there's a few things i'm struggling with. I've just started working through a book "Beginning Game Programming", and while i understand 90% of the code itself, it's little things like the types of files (.h, .o etc), indeciferable compiler errors, loopbacks, ::, etc, should i buy a C++ book and start from scratch, or will i pick these things up as i go? I've looked through a LOT of tutorials, and all the ones i've read cover specifics like variables, looping, etc very clearly, except these little nuances tend to go unnoticed

One specific problem, everything I compile comes out huge, for example:

Code:

#include <iostream.h>

int main()
{
char x;
cout << "Type c and press enter";
cin >> x;
}

comes out at 463kb. I'm using the latest Bloodshed Dev-C++, everything at default. what might i be doing wrong before i get into making bigger stuff? Also, although it's compiling fine, i get warnings like "...at least one antiquated or deprecated header..."

i hope at least something i said has made sense :S sorry

09-02-2006

twomers

>> #include <iostream.h>
Change that to <iostream>, and put a

using namespace std;

on the next line (for simplicity. You'll understand what that is later, don't worry about it for the moment). Main() should also return something. Normally, one returns 0, so before the final }, just throw in a 'return 0;'

These won't help you with the size of your file, but it's just something to keep in mind. deprecated header, that's iostream.h

You know the way in php you can include "myFile.whatever"; and you have access to that file? You can get functions from it if it's another PHP file etc, well, that's the same thing with the #include <whatever>. The .h specifies that it's a header file.

09-02-2006

mikahell

I don't really know about this compiler, but I think the message justs means that your header is just ...deprecated. Maybe it means that you shouldn't write ".h" in it, as it is a standard header file, you don't really need to add the ".h" to "iostream". Most of the standard headers found out in your compiler's directory only require you to write "<file>" without writting the extension...

Code:

#include <iostream> //Good#include <iostream.h> //Not good

As for the .exe size, it sure looks big, but if your app is compiled under debugging settings, it will always be bigger. Once your app is fully debugged, you just use "releasing" settings so your app will optimize itself, and at the same time, taking less space on your drive.

I don't know how this apply to your compiler, but normally you can't debug your app while you have settings that enables optimization for your program.

Hope that's clear, don't give up on doing this: it is certain that you can buy a book, but if you are able to understand the examples you see, maybe you will be able to get through it!

Edit: Sorry if some things have already been said!

09-02-2006

dangermouse

aha, i knew i was missing something with the namesapces, thank you :) i'd read that line before, "using namespace std" but didn't know what it applied to, without it, it claims that the function isn't declared, so obviously the header is using that namespace to declare the functions.

is there anything special about a header file? in php, an include is just another php file, can contain anything, but i get the impression .h files are somehow special?

09-02-2006

mikahell

Not really different from ".cpp", or ".c" files, though they mostly contain class definitions and functions, though that's just some sort of convention: you put the stuff you don't want to see in your main ".cpp" file in headers, so you can easily see what you'r doing.

You can, but you are not forced to use a namespace, it is just a way to avoid writting the namespace for every occurences of functions / classes that are in the namespace, such as this:

okay, i can't find anything regarding debugging, but i found some optimization setting that dropped it to 250kb, so it's obviously a Dev-C++ thing, not the code, so i'll read through the help file tomorow, thanks for pointing me in the right direction :)

09-02-2006

dangermouse

aha, so that's what the :: means! it's all starting to come together now :)

so, just to clarify, your "main()" file will be .cpp, and and any other included code, function definiteions etc, you put in a .h file? that makes sense, thanks :)

thank you guys, unbelievably helpful forum!

09-02-2006

bikr692002

That's odd, I have a 475 kb exe.... but that should be normal for me since I have a GUI and 100++ lines of code, I don't know why your getting your file so large, you prob enabled debugging without knowing it.

09-02-2006

mikahell

That's good! Now I just forgot one thing, you should use "using namespace std;" but you don't have to use the whole namespace. You can choose to only use the std members that you only need, that are for now cin and cout.

Code:

using std::cout;using std::cin;

Though if you have too many, just include the whole lot of it. I don't think it will change the .exe file size, but as you are learning, it will probably help to know in which namespace are functions XYZs...

09-02-2006

mikahell

Quote:

Originally Posted by bikr692002

That's odd, I have a 475 kb exe.... but that should be normal for me since I have a GUI and 100++ lines of code

I'd say that it also depends about if your functions are inlined, and also maybe if your code has many lines, it doesn't mean that there is not a lot of comments... But normally that should be proportional :)

09-02-2006

dangermouse

LOTS to remember, namespaces, object instances, pointer, it's a whole world above PHP, but i'm kind of enjoying it :)

The reason I'm learning is I've always, always, wanted to make a game, and FlashMX is just agonisingly inneficient, and I've got a professional animator friend that wants to get onboard too, so i'll keep you all posted :) (and i'm certain i'll be asking more questions too). if i can find a few spare dollars, i might invest in a pure c++ book

09-02-2006

ZuK

Quote:

in php, an include is just another php file, can contain anything, but i get the impression .h files are somehow special?

In general there is no difference. #include does the same as in php ( I guess ) The preprocessor just replaces the include directive with the contents of the file before it is passed to the compiler.
But then yes .h files are somehow different from other source files. That is because of how c or c++ compilers work. A c compiler doesn't produce any directly executable code, it only compiles source modules to object-modules and a linker takes care of producing the executable. You generally want a header to be reusable when compiling different source-modules. So you have to make shure that a header doesn't include any actual code ( other then inline declared code ). It should contain only declarations. Also you have to protect the contents of the header against multiple inclusions ( look up "include guards" ). And it gets a little more complicated if you want to use a c++ compiler to use headers that were written for c libraries ( look up extern "C" and name mangling ).
Kurt

09-02-2006

mikahell

Quote:

FlashMX is just agonisingly inneficient

Well, it is certain that Flash is slow and stuff, but that's because it is really easy to work
with. But when you step in the C++ programming for games, you will realize very fast that's a lot of headaches. C++ is really low-level compared to Flash, as you'll find out quickly.

But consider that when you are able to create a game out of C++, you're far more proud of your work since it is harder. But if you need some basic 2d engine, I'd suggest you the SDL library, though maybe you will have problems with using library and things since you seem to be at the point of understanding includes... But here is the link: http://www.libsdl.org/index.php

Cheers

09-02-2006

twomers

>> it's a whole world above PHP
There are striking similarities bwtween C and PHP! I mean fopen, fgets, fclose etc etc etc. But pointers and all that kind of jazz, yeah.

>> LOTS to remember, namespaces, object instances, pointer,
It may seem daunting at first, but it's not really all that hard to remember things like that. I mean, with GUI, I don't think anyone memorises most of main!! Just copy and paste. Well, that's in my limited windows programming experience. I've also found that those exe's are much smaller than console ones, oddly enough. I don't get it, but there we go.

09-02-2006

dangermouse

Thank you guys, i really appreciate you taking the time to help clarify those bits and pieces for me :) now it's just gona be a case of practise practise practise :)