-   Programming (
-   -   "undefined reference"linker error for static field w/ C++ (

astorm 11-12-2005 01:05 AM

"undefined reference"linker error for static field w/ C++
I get the linker error

/tmp/ccHfwmmN.o(.text+0x12): In function `Test::Foo()':
: undefined reference to `Test::signList'


class Test {
  static vector<string> bar;
  static void Foo();

void Test::Foo()
  bar.push_back("donut snack");

int main()

I tried initializing bar to NULL, but then the compiler says that bar is undeclared. Any help at all would be greatly appreciated.

Thanks in advance,

paulsm4 11-12-2005 01:51 AM

You only "declared" bar. Somewhere, you need to define (allocate space for) the actual variable "Test::bar".

coldAndUgly 11-12-2005 01:55 AM

Hi, both static member functions and data members must be defined somewhere.
Add this definition after the class.

vector<string> Test::bar;
Only constant integral static members may be initialised.

class Test {
        static const int i = 0;

astorm 11-12-2005 10:06 PM

Thanks a lot for your help. It works now,

paulsm4 11-12-2005 10:14 PM

Cool! That particular subtlety bit me the first time I played with static members, too!

jpmueller 08-27-2008 04:00 AM

I had that problem too, it works perfect now. Thank you!

All times are GMT -5. The time now is 01:30 AM.