Jump to content
  • Advertisement
Sign in to follow this  
breakspirit

c++ help me organize my includes correctly

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

[heading]main.cpp[/heading]

#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()


[heading]gamesimulation.h[/heading]

#ifndef GAME_SIMULATION
#define GAME_SIMULAION

#include "Player.h"

class GameSimulation
{all the gamesimulation stuff (definitions)}

GameSimulation implementation
#endif



[heading]player.h[/heading]
class Player
{all the player stuff (definitions)}

Player implementation



[heading]netcode.h[/heading]

#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



[heading]netcode.cpp[/heading]

#include "NetCode.h"
netcode method implementations



[heading]variables.h[/heading]
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.

Share this post


Link to post
Share on other sites
Advertisement
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..

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites

[color=#1C2837][size=2]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.

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


[color="#1C2837"]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
[/quote]

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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="#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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!