Author Topic: Unresolved References to static variables  (Read 4838 times)

pjmavcom

  • Guest
Unresolved References to static variables
« on: November 18, 2007, 10:26:32 pm »
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 ===


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

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

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.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5490
Re: Unresolved References to static variables
« Reply #1 on: November 18, 2007, 10:40:36 pm »
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.
« Last Edit: November 19, 2007, 08:02:17 am by killerbot »

pjmavcom

  • Guest
Re: Unresolved References to static variables
« Reply #2 on: November 18, 2007, 10:49:27 pm »
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.

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Unresolved References to static variables
« Reply #3 on: November 18, 2007, 11:47:08 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
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;
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline Belgabor

  • Multiple posting newcomer
  • *
  • Posts: 91
Re: Unresolved References to static variables
« Reply #4 on: November 19, 2007, 02:16:40 am »
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.

pjmavcom

  • Guest
Re: Unresolved References to static variables
« Reply #5 on: November 19, 2007, 02:21:17 am »
Tdragon, thanks. Thats working now. Id about given up on this after what Killerbot said. Belgabor, thanks too.
Is there somewhere else I can post C++ specific questions? Or another site/forum with knowledgeable people on it somewhere?

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5490
Re: Unresolved References to static variables
« Reply #6 on: November 19, 2007, 07:54:01 am »
yes, correct : the header part only works for const, I overlooked this. So stupid I even give tips to not use those when they are not const, duh ...

So for clarity :
class X
{
protected:
  const int CentsInADollar = 100;
}

and no linker will complain.

but my b) answer was correct though  8) [by similar I didn't mean to copy the lines, but do a similar action, like TDragon showed, I took this knowledge for granted ...]

Offline JGM

  • Lives here!
  • ****
  • Posts: 518
  • Got to practice :)
Re: Unresolved References to static variables
« Reply #7 on: November 23, 2007, 04:17:40 am »
...
c++ programming forums I could post this kind of thing to instead?
...

There are plenty, search on google here are some sites:

http://www.programmersheaven.com/mb/
http://www.gidforums.com/f-28.html
http://www.codeproject.com/

Here are some free ebooks:  :)

http://www.freeprogrammingresources.com/cppbooks.html

And the best book out there from the creator of c++ ( for me is the better):  :D

http://www.research.att.com/~bs/3rd.html