Errors with code

Started by
13 comments, last by Rhaal 19 years, 6 months ago
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?
______________________________My website: Quest Networks
Advertisement
Quote:Original post by jakpandora
#include "game.cpp"


Usually you should include header files (*.h), not implementation files (*.cpp) in your header.

Maybe you should also read this:

http://www.gamedev.net/reference/articles/article1798.asp
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.
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]
#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.
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.
______________________________My website: Quest Networks
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)
______________________________My website: Quest Networks
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
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
could someone help?
______________________________My website: Quest Networks
The problem is probably in the other file.

This topic is closed to new replies.

Advertisement