User forums > Using Code::Blocks
Unresolved References to static variables
pjmavcom:
Hi. Im designing a class with 3 static variables, among other things. When I try to compile this with GCC Im getting this error...
--- Code: --- Warning: .drectve `/DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"uuid.lib" ' unrecognized
obj\Debug\C_Object.o: In function `ZN10C_ObjectC2Ev':
\C_Object.cpp:22: undefined reference to `C_Object::m_count'
obj\Debug\C_Object.o: In function `ZN10C_ObjectC1Ev':
\C_Object.cpp:22: undefined reference to `C_Object::m_count'
obj\Debug\C_Object.o: In function `ZN10C_ObjectD2Ev':
\C_Object.cpp:27: undefined reference to `C_Object::m_count'
obj\Debug\C_Object.o: In function `ZN10C_ObjectD1Ev':
\C_Object.cpp:27: undefined reference to `C_Object::m_count'
obj\Debug\C_Object.o: In function `ZN10C_Object8GetCountEv':
\C_Object.cpp:32: undefined reference to `C_Object::m_count'
obj\Debug\C_Object.o: In function `ZN10C_Object8GetIndexEv':
\C_Object.cpp:37: undefined reference to `C_Object::m_index'
obj\Debug\C_Object.o: In function `ZN10C_Object8SetFirstEPS_':
\C_Object.cpp:88: undefined reference to `C_Object::m_pfirst'
obj\Debug\C_Object.o: In function `ZN10C_Object8GetFirstEv':
\C_Object.cpp:93: undefined reference to `C_Object::m_pfirst'
obj\Debug\C_Object.o: In function `ZN10C_Object7SetLastEPS_':
\C_Object.cpp:98: undefined reference to `C_Object::m_plast'
obj\Debug\C_Object.o: In function `ZN10C_Object7GetLastEv':
\C_Object.cpp:103: undefined reference to `C_Object::m_plast
=== Build finished: 10 errors, 1 warnings ===
--- End code ---
The code in the header file goes like this...
--- Code: ---// C_Object.h
class C_Object
{
protected:
static int m_count;
static C_Object* m_pfirst;
static C_Object* m_plast;
public:
int GetCount();
C_Object* GetFirst();
C_Object* GetLast();
}
etc...
--- End code ---
And in the source file...
--- Code: ---// C_Object.cpp
#include "C_Object.h"
C_Object::C_Object()
{
m_count++;
}
C_Object::~C_Object()
{
m_count--;
}
etc...
--- End code ---
I dont understand what the problem is with this. I can guess theres something wrong with the static variables, but what?
Im using the GCC compiler on a Windows XP machine. Anything else needed about this?
Also on a side note, that warning up there. What is that about?
Thanks for the help.
killerbot:
some remarks :
1) this is not a general programming board, what you are asking about, is regular c++
2) you only declared the static's you didn't define them :
solutions
a) in the header
static int m_count = 0;
static C_Object* m_pfirst = 0;
static C_Object* m_plast = 0; EDIT : this is NOT correct (only for const)
b) or you do the 'similar' actions in the cpp file
3) in case you want to reference count the number of objects, note that this code is not thread safe
Basically those static's are just plain global variables, which is not a good idea, and certainly not when you write to them. There are better solutions to count your number of instances
4) having 'member' variables as protected is not good, is nearly as bad as public. There are sound reasons to have member methods that are protected, but it is better to avoid these too. Every class which inherits from your class has access to the protected variables and as such you violate the open/close principle. You can not change that variables name (or remove it) anymore without breaking the classes that inherited from your class. So basically you showed your internals, just like a public would do. Inheritance is mostly about behavior and not about state.
pjmavcom:
Thanks for the quick reply killerbot. As you can see Im pretty new to c++. I usually let the error messages I get let me know how I messed up or misused the code, but linker errors are pretty confusing to say the least. Could you recommend any c++ programming forums I could post this kind of thing to instead?
Again I appreciate the help.
TDragon:
--- Quote from: killerbot on November 18, 2007, 10:40:36 pm ---a) in the header
static int m_count = 0;
static C_Object* m_pfirst = 0;
static C_Object* m_plast = 0;
b) or you do the similar actions in the cpp file
--- End quote ---
This is not quite correct; it merely declares 3 compilation-unit-unique global variables. To properly define your static member variables, you must do the following in your source file:
--- Code: ---int C_Object::m_count = 0;
C_Object* C_Object::m_pfirst = 0;
C_Object* C_Object::m_plast = 0;
--- End code ---
Belgabor:
To make that exact, TDragon means the cpp source file, not the header file (which is somewhat a source file as well).
If you declare them in the header file and include it from several other files, you'll again get linking errors as each inclusion will generate a new global variable with the same name.
Navigation
[0] Message Index
[#] Next page
Go to full version