• Create Account

c++ help me organize my includes correctly

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

32 replies to this topic

#1breakspirit  Members

Posted 07 October 2011 - 05:09 PM

I'm setting up classes and I'm having a hard time getting my includes to stop giving errors. Here's where I'm at:

#pragma once
#include <stdio.h>
#include <string>
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"  // MessageID
#include "GetTime.h"
#include <iostream>
#include <vector>
#include <fstream>
#include "Kbhit.h"

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

#include "NetCode.h"
Player			newPlayer;		// a player object to give to new players.
GameSimulation	game;
NetCode netConnection;

main()


#ifndef GAME_SIMULATION
#define GAME_SIMULAION

#include "Player.h"

class GameSimulation
{all the gamesimulation stuff (definitions)}

GameSimulation implementation
#endif


class Player
{all the player stuff (definitions)}

Player implementation


#ifndef NET_CODE
#define NET_CODE

#pragma once
#include <stdio.h>
#include <string>
#include "RakPeerInterface.h"
#include "MessageIdentifiers.h"
#include "BitStream.h"
#include "RakNetTypes.h"  // MessageID
#include "GetTime.h"
#include <iostream>
#include <vector>
#include <fstream>
#include "Kbhit.h"

using namespace std;

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

class NetCode
{all the netcode class stuff (definitions)}
#endif


#include "NetCode.h"
netcode method implementations


global variables that I want everything to be able to access; things like enums and handles

My stuff is a little jumbled around and there's some stuff commented out because I've been playing with it for like an hour trying to get it to work and now it's a bit of a mess. I hope I've made it clear what I'm trying to do.

I'm familiar with how includes work in the sense that they simply substitute in whatever they're including into the original source page, but apparently I'm missing something because I'm getting all sorts of errors of files not knowing about variable types and such. I'd paste them, but there's hundreds and I know that it's just trying to tell me that my includes aren't working right.
Basically, variables.h need to be available everywhere, the netcode needs the gamesimulation to pass it messages, and gamesimulation needs to see players. Main needs to be able to access the whole shebang. Thanks for any help you guys can give.

#2SiCrane  Moderators

Posted 07 October 2011 - 05:12 PM

#3Juliean  GDNet+

Posted 07 October 2011 - 05:19 PM

Cant discuss all your code now, just some suggestions:

- you cant for example include a.h into b.h and then b.h into a.h. Will give you errors en masse. Make sure your inclusion-list doesnt do that unintentionaly.

-#pragma once on top of every header file can alse redude errors. Howerer make sure there is no a->b->a-relationship in your classes..

#4breakspirit  Members

Posted 07 October 2011 - 05:36 PM

Thanks for the responses. I have read that article and am rereading it to see if I've missed some nugget of useful information. I see nowhere in which I have a cyclical include. My answer could probably be ascertained if someone can tell me why NetCode.cpp doesn't know what game or newPlayer are even after I move #include "NetCode.h" to after their declarations in main.cpp. Basically, in both of the following cases, NetCode.cpp knows nothing about game or newPlayer:

#include "NetCode.h"
Player newPlayer;
GameSimulation	game;
NetCode netConnection;

Player newPlayer;
GameSimulation	game;
NetCode netConnection;
#include "NetCode.h"


#5pulpfist  Members

Posted 07 October 2011 - 05:37 PM

I'd paste them, but there's hundreds and I know that it's just trying to tell me that my includes aren't working right.

In cases like this, the error messages at the top is usually the relevant ones.
You might want to paste those

#6pulpfist  Members

Posted 07 October 2011 - 05:45 PM

I think you should use either

#ifndef ?
#define ?
...
#endif

or

#pragma once

but not both.

Also make sure that your inclusion guards is unique for each header.
Copy and pasting can sometimes lead to header files using the same inclusion guards, something that could cause problems.

#7breakspirit  Members

Posted 07 October 2011 - 05:50 PM

I'd paste them, but there's hundreds and I know that it's just trying to tell me that my includes aren't working right.

In cases like this, the error messages at the top is usually the relevant ones.
You might want to paste those

Yeah, I've been trying to go from the top down. Here's the current first bunch of errors:
1>...\gamesimulation.h(20): error C2143: syntax error : missing ';' before '<'                   		<--This part refers to a vector of Players I'm trying to create within GameSimulation
1>...\gamesimulation.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\gamesimulation.h(20): error C2238: unexpected token(s) preceding ';'
1>>...\gamesimulation.h(40): error C2065: 'playerList' : undeclared identifier
1>>...\gamesimulation.h(40): error C2228: left of '.push_back' must have class/struct/union
1>          type is ''unknown-type''
1>>...\gamesimulation.h(41): error C2065: 'playerList' : undeclared identifier
1>>...\gamesimulation.h(51): error C2065: 'gameWorld' : undeclared identifier
1>>...\netcode.cpp(95): error C2065: 'newPlayer' : undeclared identifier
1>>...\netcode.cpp(95): error C2227: left of '->playerGuid' must point to class/struct/union/generic type
1>          type is ''unknown-type''
1>>...\netcode.cpp(96): error C2065: 'game' : undeclared identifier
1>>...\netcode.cpp(96): error C2228: left of '.addNewPlayer' must have class/struct/union
1>          type is ''unknown-type''
1>>...\netcode.cpp(96): error C2065: 'newPlayer' : undeclared identifier

#8breakspirit  Members

Posted 07 October 2011 - 05:55 PM

I think you should use either
#ifndef ?
#define ?
...
#endif
or
#pragma once
but not both.
Also make sure that your inclusion guards is unique for each header.
Copy and pasting can sometimes lead to header files using the same inclusion guards, something that could cause problems.

Alright, I went ahead and made sure all files had only one inclusion guard. They are unique for each file. I also went ahead and made sure Variables.h has inclusion guards. Same errors unfortunately, but thanks for the suggestions.

#9pulpfist  Members

Posted 07 October 2011 - 06:11 PM

That first error was a pretty good hint

Take a close look at this:

#ifndef GAME_SIMULATION
#define GAME_SIMULAION

edit:
The error itself is actually quite obscure, but it basically indicates that the compiler has stumbled over a data type it doesn't recognize

#10breakspirit  Members

Posted 07 October 2011 - 06:30 PM

That first error was a pretty good hint

Take a close look at this:

#ifndef GAME_SIMULATION
#define GAME_SIMULAION

edit:
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.

#11pulpfist  Members

Posted 07 October 2011 - 06:56 PM

That first error was a pretty good hint

Take a close look at this:

#ifndef GAME_SIMULATION
#define GAME_SIMULAION

edit:
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.

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?

#12breakspirit  Members

Posted 07 October 2011 - 07:00 PM

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.

#13pulpfist  Members

Posted 07 October 2011 - 07:04 PM

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.

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

#14breakspirit  Members

Posted 07 October 2011 - 07:10 PM

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

Nah it's in there. =/

#15pulpfist  Members

Posted 07 October 2011 - 07:15 PM

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

Nah it's in there. =/

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

#16breakspirit  Members

Posted 07 October 2011 - 07:31 PM

Alright, here's all the source. Thanks again for your help.

#17SiCrane  Moderators

Posted 07 October 2011 - 07:46 PM

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

#18breakspirit  Members

Posted 07 October 2011 - 07:48 PM

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.

#19SiCrane  Moderators

Posted 07 October 2011 - 07:50 PM

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.

#20pulpfist  Members

Posted 07 October 2011 - 07:53 PM

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.

@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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.