Skip to main content
gamedev.net
🔒 Locked

Global Object Variable problem...

Started by philkills 6 replies 1k views
Original Post
philkills
philkills
Hi all, i have been trying to create a global object so that it can be manipulated by a variety of classes within my program.... Only problem is i keep getting a variety of errors and i have no idea why =/ i have created a file called GlobalVar.h to store the variable..(and any others i choose to in the future) here it is: #ifndef GLOBAL_VAR_H #define GLOBAL_VAR_H #include "TileMapTool.h" // Globals extern TileMapTool* g_pTMData; #endif TileMapTool is a class... but all i get is these errors: error C2143: syntax error : missing ';' before '*' error C2501: 'g_pTMData' : missing storage-class or type specifiers does anyone have any idea why? its quite annoying >_< thx in advance ^_^
ToohrVyk
ToohrVyk
Does "TileMapTool.h" include "GlobalVar.h" ? It shouldn't.
NotAYakk
NotAYakk
Most likely, "TileMapTool.h" is including "GlobalVar.h".

Some .cpp file #includes "TileMapTool.h". It has a header-guard. It then includes "GlobalVar.h", which includes "TileMapTool.h", which is skipped thanks to the header-guard, and the symbol TileMapTool isn't define.

Might I suggest:

#ifndef GLOBAL_VAR_H#define GLOBAL_VAR_H#include "TileMapTool.h"class TileMapTool;// Globalsextern TileMapTool* g_pTMData;#endif


And might I also suggest using namespaces and hiding global variables in accessor functions?

Ie:

#ifndef GLOBAL_VAR_H#define GLOBAL_VAR_H#include "TileMapTool.h"namespace MyProject {  class TileMapTool;  // Globals  TileMapTool* GlobalTileMapTool();}#endif

Fruny
Fruny
class TileMapTool;extern TileMapTool* g_pTMData;


Would be enough. Multiple declarations aren't an error, so long as all the declarations are identical, so there technically isn't a need for header guards in this particular case. Furthermore declaring a pointer to some type doesn't require the type itself to be defined, so you don't need to include TileMapTool.h either.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
philkills
philkills
what i have done is create a file called StdAfx.h with all the includes in it


#ifndef STDAFX_H
#define STDAFX_H

#include
#include
#include
#include
#include
#include
#include
#include
#include "ConstDefinitions.h"
#include "MirMap.h"
#include "TileMapTool.h"

#endif

then in all the header files i simply #include "StdAfx.h"...

i thought that aslong as something has already been loaded... then its fine

update:

Added class TileMapTool;

now i get this error:
MirToSomaSpl.obj : error LNK2001: unresolved external symbol "class TileMapTool * g_pTMData" (?g_pTMData@@3PAVTileMapTool@@A)

lol...
okonomiyaki
okonomiyaki
Quote:
Original post by philkills

now i get this error:
MirToSomaSpl.obj : error LNK2001: unresolved external symbol "class TileMapTool * g_pTMData" (?g_pTMData@@3PAVTileMapTool@@A)



You have to actually declare your variables somewhere. All the extern statement is doing is telling everything that this variable is declared, but it has external linkage. Make a GlobalVar.cpp or something that includes just this:

TileMapTool* g_pTMData;
philkills
philkills
thx alot mate ^_^
Zahlman
Zahlman
Quote:
Original post by philkills
#include
#include
#include
#include


Hold on, do you think you're writing C, or C++?

You do know that std::string lives in rather than , yeah?

Quote:

#include
#include
#include


WTF are *those*? Not the C++ standard, that's for sure. Hint: you've had 8 whole years to get clued in. The standard C++ library now uses header names without a .h extension, and makes use of this lovely feature of the language called "namespaces"; it puts all those symbols into the std:: namespace.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.