• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
breakspirit

c++ help me organize my includes correctly

32 posts in this topic

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]
[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"

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

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

main()
[/code]

[heading]gamesimulation.h[/heading]
[code]
#ifndef GAME_SIMULATION
#define GAME_SIMULAION

#include "Player.h"

class GameSimulation
{all the gamesimulation stuff (definitions)}

GameSimulation implementation
#endif
[/code]


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

Player implementation
[/code]


[heading]netcode.h[/heading]
[code]
#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
[/code]


[heading]netcode.cpp[/heading]
[code]
#include "NetCode.h"
netcode method implementations
[/code]


[heading]variables.h[/heading]
[code]global variables that I want everything to be able to access; things like enums and handles[/code]

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.
0

Share this post


Link to post
Share on other sites
Have you read [url=http://www.gamedev.net/page/resources/_/technical/general-programming/organizing-code-files-in-c-and-c-r1798] this article[/url]?
0

Share this post


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

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:

[code]#include "NetCode.h"
Player newPlayer;
GameSimulation game;
NetCode netConnection;[/code]

[code]
Player newPlayer;
GameSimulation game;
NetCode netConnection;
#include "NetCode.h"
[/code]
0

Share this post


Link to post
Share on other sites
[quote]
[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. [/size][/color]

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

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.
0

Share this post


Link to post
Share on other sites
[quote name='pulpfist' timestamp='1318030622' post='4870311']
[quote]
[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. [/size][/color]

[/quote]
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:
[code]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[/code]
0

Share this post


Link to post
Share on other sites
[quote name='pulpfist' timestamp='1318031148' post='4870312']
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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
That first error was a pretty good hint [img]http://public.gamedev.net/public/style_emoticons/default/laugh.gif[/img]

Take a close look at this:

[color="#1C2837"][size="2"][color="#880000"]#ifndef[/color][color="#000000"] GAME_SIMULATION
[/color][color="#880000"]#define[/color][color="#000000"] GAME_SIMULAION[/color][/size][/color]
[color="#1C2837"][size="2"] [/size][/color]
[color="#1C2837"][size="2"][color="#000000"]edit:[/color][/size][/color]
[color="#1C2837"][size="2"][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[/color][/size][/color]
0

Share this post


Link to post
Share on other sites
[quote name='pulpfist' timestamp='1318032666' post='4870319']
That first error was a pretty good hint [img]http://public.gamedev.net/public/style_emoticons/default/laugh.gif[/img]

Take a close look at this:

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

[color="#1C2837"][size="2"][color="#000000"]edit:[/color][/size][/color]
[color="#1C2837"][size="2"][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[/color][/size][/color]
[/quote]

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

[code]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[/code]

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:

[code]#include "Variables.h"
//#include "Player.h"
//#include "GameSimulation.h"

class GameSimulation;
class Player;

extern Player *newPlayer;
extern GameSimulation game;

class NetCode
{}[/code]

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.
0

Share this post


Link to post
Share on other sites
[quote name='breakspirit' timestamp='1318033800' post='4870322']
[quote name='pulpfist' timestamp='1318032666' post='4870319']
That first error was a pretty good hint [img]http://public.gamedev.net/public/style_emoticons/default/laugh.gif[/img]

Take a close look at this:

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

[color="#1C2837"][size="2"][color="#000000"]edit:[/color][/size][/color]
[color="#1C2837"][size="2"][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[/color][/size][/color]
[/quote]

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

[code]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[/code]

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:

[code]#include "Variables.h"
//#include "Player.h"
//#include "GameSimulation.h"

class GameSimulation;
class Player;

extern Player *newPlayer;
extern GameSimulation game;

class NetCode
{}[/code]

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 [i]newPlayer[/i] and [i]game[/i] exists? The extern keyword tells the linker that they will be found in some other unit, but which?
0

Share this post


Link to post
Share on other sites
[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 [i]newPlayer[/i] and [i]game[/i] exists? The extern keyword tells the linker that they will be found in some other unit, but which?
[/quote]

Yeah, I've tried including GameSimulation.h in netcode.cpp and I get link errors saying this kinda stuff:
[code]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[/code]

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

[code]#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;

[/code]

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

Share this post


Link to post
Share on other sites
[quote name='breakspirit' timestamp='1318035640' post='4870325']
[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 [i]newPlayer[/i] and [i]game[/i] exists? The extern keyword tells the linker that they will be found in some other unit, but which?
[/quote]

Yeah, I've tried including GameSimulation.h in netcode.cpp and I get link errors saying this kinda stuff:
[code]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[/code]

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

[code]#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;

[/code]

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?
0

Share this post


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

Nah it's in there. =/
0

Share this post


Link to post
Share on other sites
[quote name='breakspirit' timestamp='1318036226' post='4870328']
[quote name='pulpfist' timestamp='1318035885' post='4870326']
Um, you didn't forget to include netcode.h in netcode.cpp did you?
[/quote]

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 [img]http://public.gamedev.net/public/style_emoticons/default/blink.gif[/img]
0

Share this post


Link to post
Share on other sites
Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1318038384' post='4870338']
Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.
[/quote]

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.
0

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1318038384' post='4870338']
Do not put non-inline function definitions in header files. Non-inline function definitions go in source files.
[/quote]

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 [i]std::string[/i]
0

Share this post


Link to post
Share on other sites
Thanks for the input guys. I created the .cpp files and am still getting the same errors with the addition of the fact that gamesimulation has mysteriously forgotten about Variables.h. When I manually included it in gamesimulation.h, I got different errors. So, I included the current code and here's the errors:
[code]1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.cpp(101): error C2027: use of undefined type 'GameSimulation'
1> c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.h(21) : see declaration of 'GameSimulation'
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\netcode.cpp(101): error C2228: left of '.addNewPlayer' must have class/struct/union
1> GameSimulation.cpp
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.h(17): error C2065: 'GAME_WIDTH' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.h(17): error C2065: 'GAME_HEIGHT' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.h(19): error C2143: syntax error : missing ';' before '<'
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.h(19): error C2238: unexpected token(s) preceding ';'
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(7): error C2065: 'GAME_WIDTH' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(10): error C2065: 'GAME_HEIGHT' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(12): error C2065: 'GAME_HEIGHT' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(15): error C2065: 'GAME_WIDTH' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(21): error C2065: 'playerList' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(21): error C2228: left of '.push_back' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(22): error C2065: 'playerList' : undeclared identifier
1>c:\users\kev\desktop\game stuff\raknet stuff\raknettest\raknettest\gamesimulation.cpp(29): error C2065: 'isServer' : undeclared identifier[/code]
0

Share this post


Link to post
Share on other sites
If you actually want to use a class type, you can't get by with just a forward declaration, you need the full definition. So #include the relevant header file when you use the variable. Just to make things clear: just because one of your source file includes a header doesn't mean that every other file will see the contents. Every file that needs the contents of the header needs to include that header.
0

Share this post


Link to post
Share on other sites
You forward declare [b]GameSimulation[/b] in [b]NetCode.h[/b], but you still need to [b]include GameSimulation.h[/b] in [b]NetCode.cpp[/b] in order to use its methods. [b]GameSimulation.h[/b] doesn't know about [b]GAME_WIDTH[/b] or [b]GAME_HEIGHT[/b] because the include line for [b]Variables.h[/b] is commented out. Use [b]std::vector[/b] instead of vector on line 19 of [b]GameSimulation.h[/b]. [b]isServer[/b], used in [b]GameSimulation::setUpNewPlayer()[/b], is not available because the include line for [b]Variables.h[/b] is commented out in [b]GameSimulation.h[/b].
0

Share this post


Link to post
Share on other sites
I'm down to just the errors with gamesimulation.h and cpp not being able to see the contents of Variables.h. If I uncomment out #include "Variables.h" I get different errors, as shown below:

[code]1>main.obj : error LNK2005: "bool isServer" (?isServer@@3_NA) already defined in GameSimulation.obj
1>main.obj : error LNK2005: "void * hStdout" (?hStdout@@3PAXA) already defined in GameSimulation.obj
1>main.obj : error LNK2005: "unsigned __int64 startTime" (?startTime@@3_KA) already defined in GameSimulation.obj
1>NetCode.obj : error LNK2005: "bool isServer" (?isServer@@3_NA) already defined in GameSimulation.obj
1>NetCode.obj : error LNK2005: "void * hStdout" (?hStdout@@3PAXA) already defined in GameSimulation.obj
1>NetCode.obj : error LNK2005: "unsigned __int64 startTime" (?startTime@@3_KA) already defined in GameSimulation.obj
1>C:\Users\Kev\Desktop\Game stuff\Raknet stuff\RakNetTest\Debug\RakNetTest.exe : fatal error LNK1169: one or more multiply defined symbols found[/code]

That makes me think that I'm including Variables.h too many times somewhere. Player.h is not including Variables currently and when it does, I get more errors of the same sort.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0