Public Group

# Errors with code

This topic is 5461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I decide to make my next project have multiple souce files for the first time. however, after writing a little test to make sure I was doing eveyrhting right, I got a few errors. heres my code: game.cpp
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "game.h"
using namespace std;

int main()

{
cout << "how ya doin" << gold << endl;
system("pause");
return 0;
}


game.h
#include <cstdlib>
#include <iostream>
#include <cmath>
#include "game.cpp"
using namespace std;

int main()

{
int gold = 50;
int health = 50;
return 0;
}


could someone help?

##### Share on other sites
Quote:
 Original post by jakpandora#include "game.cpp"

Maybe you should also read this:

http://www.gamedev.net/reference/articles/article1798.asp

##### Share on other sites
There's a lot wrong with that code. First off, you have too many main functions. You only need one, which should be in your main file (game.cpp, in this case). Next, you have a circular include (game.cpp includes game.h, which includes game.cpp, which includes ...). The .cpp files should #include the .h files, never the other way around (#include works as if you had typed the entire included file instead of writing include). Also, you should never define global variables in a header file. Declare them with extern, then define them in a .cpp file.

Here's my verion:

game.h
#include <cstdlib>#include <iostream>#include <cmath>using namespace std;extern int gold;extern int health;

game.cpp
#include "game.h"int gold = 50;int health = 50;int main(){	cout << "how ya doin" << gold << endl;	system("pause");	return 0;}

Hope that helps.

-RC

Edit: I don't know how to make code show up in those white boxes. Anybody want to PM me with instructions?
Edit again: Never mind - I figured it out.

##### Share on other sites
You shoiuld read up how headers work again, you don't quite seem to get how they work.

Heres how your code should look

game.cpp

#include "game.h"using namespace std;int gold = 50;int health = 50;int main(){	cout << "how ya doin" << gold << endl;	system("pause");	return 0;}

game.h

#include <cstdlib>#include <iostream>#include <cmath>extern int gold;extern int health;

This code isn't great though - in this case there isn't much need to use a header. A header is only useful when more than one .cpp file needs to access the same data or use the same structures. The .h file defines a structure. The .cpp file contains the actual implementation.

EDIT: Too slow [grin]

##### Share on other sites
#include actually does a simple copy-paste of the included file into the current file for the duration of the compiling. In your case, the copy-paste step just never ends, because the pasted part contains another #include...

The following guidelines are useful if you don't want to be sorting errors on your own:

- Never include a cpp file anywhere.
- Include h files in cpp files.
- Put function definitions into cpp files:
void main( void ) { }
- Put function declarations into h files:
void main( void );

Something you can't do:

- Define a function twice: if you write the main function twice, the compiler or linker will have to choose only one of them, and will simply give up (it's not their job to choose).

People usually need multiple files in the following case:

You need to have two functions, A() and B(). A() needs to call B(), so you declare B() in a .cpp file, before A() is defined.

Now, it's easier to put all declarations in a .h file, and include the file in the .cpp file.

But now, you don't need to actually write what B() does (ie define it) in the same file as the one you use it: you can write it somewhere else, and the linker will stick everything together. So you create another cpp file, write the definition of B() inside (and include headers containing declarations of things used by B() ).

All you have to do is compile both cpp files, link them, and it works.

##### Share on other sites
ok, thanks guys! I know a header file isnt useful with my present code; it was just a sample to see if I was doing everything right. ill have to read that gamedev article.

##### Share on other sites
thats odd, I fixed my code and got more errors. heres my code

#include <cstdlib>#include <iostream>#include <cmath>using namespace std;extern int gold;extern int health;

here are the errors:

--------------------Configuration: Sentari - Win32 Debug--------------------
Compiling...
game.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\Sentari\game.cpp(9) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
C:\Program Files\Microsoft Visual Studio\MyProjects\Sentari\game.cpp(9) : error C2146: syntax error : missing ';' before identifier 'gold'
C:\Program Files\Microsoft Visual Studio\MyProjects\Sentari\game.cpp(9) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

Sentari.exe - 2 error(s), 1 warning(s)

##### Share on other sites
yeah i used to have problems with headers and why you dont include a .cpp file to make a long story short as long as the .cpp file is including the right header just include the header and the rest will take care of its self

##### Share on other sites
could someone help?

##### Share on other sites
The problem is probably in the other file.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 13
• 14