Hi. Im designing a class with 3 static variables, among other things. When I try to compile this with GCC Im getting this error...
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 ===
The code in the header file goes like this...
// 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...
And in the source file...
// C_Object.cpp
#include "C_Object.h"
C_Object::C_Object()
{
m_count++;
}
C_Object::~C_Object()
{
m_count--;
}
etc...
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.
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
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:
int C_Object::m_count = 0;
C_Object* C_Object::m_pfirst = 0;
C_Object* C_Object::m_plast = 0;