c++ help me organize my includes correctly

Started by
31 comments, last by popsoftheyear 12 years, 6 months ago

[quote name='pulpfist' timestamp='1318032666' post='4870319']
That first error was a pretty good hint laugh.gif

Take a close look at this:

[color="#1C2837"][color="#880000"]#ifndef[color="#000000"] GAME_SIMULATION
[color="#880000"]#define[color="#000000"] GAME_SIMULAION

[color="#1C2837"][color="#000000"]edit:
[color="#1C2837"][color="#000000"]The error itself is actually quite obscure, but it basically indicates that the compiler has stumbled over a data type it doesn't recognize


Haha yeah I saw that and fixed it and smacked myself. Still getting errors though. I'm down to just two. Here they are:

1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.cpp(100): error C2027: use of undefined type 'GameSimulation'
1> c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.h(22) : see declaration of 'GameSimulation'
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.cpp(100): error C2228: left of '.addNewPlayer' must have class/struct/union


I've done a lot of moving things around, but basically I think my problem lies in NetCode.h because netcode.cpp still doesn't know anything about game but mysteriously doesn't have a problem with player. Here's the relevant chunk of code:

#include "Variables.h"
//#include "Player.h"
//#include "GameSimulation.h"

class GameSimulation;
class Player;

extern Player *newPlayer;
extern GameSimulation game;

class NetCode
{}


I don't understand why it needs me to include Variables.h again. You'll notice that I tried using extern in there but it has not fixed my issues.
[/quote]

Looks like netcode.cpp is trying to use the GameSimulation class from netcode.h, which is just a forward declaration.


Its a bit hard to understand how you want to connect things from just these snippets, but anyway, have you tried to include GameSimulation.h in netcode.cpp?
And where does newPlayer and game exists? The extern keyword tells the linker that they will be found in some other unit, but which?
Advertisement

Looks like netcode.cpp is trying to use the GameSimulation class from netcode.h, which is just a forward declaration.

Its a bit hard to understand how you want to connect things from just these snippets, but anyway, have you tried to include GameSimulation.h in netcode.cpp?
And where does newPlayer and game exists? The extern keyword tells the linker that they will be found in some other unit, but which?


Yeah, I've tried including GameSimulation.h in netcode.cpp and I get link errors saying this kinda stuff:
1>NetCode.obj : error LNK2005: "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ) already defined in main.obj
1>NetCode.obj : error LNK2005: "public: __thiscall GameSimulation::GameSimulation(void)" (??0GameSimulation@@QAE@XZ) already defined in main.obj


newPlayer and game get declared in main.cpp like this:

#include "Variables.h"
#include "GameSimulation.h"

Player *newPlayer = new Player; // a pointer to a player object to give to new players.
GameSimulation game;

#include "NetCode.h"
NetCode netConnection;



I simply do not understand why netcode.cpp can not see newPlayer and game.

[quote name='pulpfist' timestamp='1318035361' post='4870323']
Looks like netcode.cpp is trying to use the GameSimulation class from netcode.h, which is just a forward declaration.

Its a bit hard to understand how you want to connect things from just these snippets, but anyway, have you tried to include GameSimulation.h in netcode.cpp?
And where does newPlayer and game exists? The extern keyword tells the linker that they will be found in some other unit, but which?


Yeah, I've tried including GameSimulation.h in netcode.cpp and I get link errors saying this kinda stuff:
1>NetCode.obj : error LNK2005: "public: __thiscall Player::Player(void)" (??0Player@@QAE@XZ) already defined in main.obj
1>NetCode.obj : error LNK2005: "public: __thiscall GameSimulation::GameSimulation(void)" (??0GameSimulation@@QAE@XZ) already defined in main.obj


newPlayer and game get declared in main.cpp like this:

#include "Variables.h"
#include "GameSimulation.h"

Player *newPlayer = new Player; // a pointer to a player object to give to new players.
GameSimulation game;

#include "NetCode.h"
NetCode netConnection;



I simply do not understand why netcode.cpp can not see newPlayer and game.
[/quote]

Um, you didn't forget to include netcode.h in netcode.cpp did you?

Um, you didn't forget to include netcode.h in netcode.cpp did you?


Nah it's in there. =/

[quote name='pulpfist' timestamp='1318035885' post='4870326']
Um, you didn't forget to include netcode.h in netcode.cpp did you?


Nah it's in there. =/
[/quote]

Yea I thought so.
If you post all your files it will be easier for us to see what the problem is. I suspect you got some inclusion spaghetti going on here blink.gif
Alright, here's all the source. Thanks again for your help.
Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.

Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.


Yeah, but I don't think that is associated with this problem. I seldom do that unless the function is highly incomplete, such as with the simulation and player functions.
Your error is a duplicate symbol error. This means that more than one definition is found in more than one source file. Because you defined the functions in the header, multiple source files are trying to export the same function definition, hence the duplicate symbols.

Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.


Yea I was about to say something like that. I assume you refer to the Player.h and GameSimulation.h files.
That could explain the "already defined" linker errors he got.

@breakspirit
I suggest you start by creating a Player.cpp and a GameSimulation.cpp file, and move all function and constructor implementations into those.
If that still doesn't work out I'll take a closer look at this in a couple of hours.

Also, as a general rule, don't say "using namespace ..." in a header file.
Doing it in a cpp file may be ok, but doing it in header files can cause problems as the project grows.

In the header files, use a fully qualified scope, like std::string

This topic is closed to new replies.

Advertisement