3

Please tell me, why gcc linker gives me the following error: "test_class::test_struct::constVar", referenced from: __ZN12lu_test_class27test_struct6constVar$non_lazy_ptr in test_class.o ?

My code (test_class.h):

class test_class
{
    struct test_struct
    {
         static const int constVar = 0;
    };
};

All references to constVar are in test_class scope in a usual static member access form: test_struct::constVar.

Ryan
  • 1,451
  • 2
  • 27
  • 36

1 Answers1

3

Provide the definition of the static member outside the class

const int test_class::test_struct::constVar;

This works for me.

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • Thanks, Prasoon! Can anybody please explain why is it necessary? – Ryan May 26 '11 at 13:08
  • @Ryan : Yes! Read [this](http://stackoverflow.com/questions/6106194/definition-of-static-const-outside-the-class-definition/6106240#6106240) answer. :) – Prasoon Saurav May 26 '11 at 13:09
  • I'm sorry to say, but the problem is not solved - I get linker error 'duplicate symbol test_class::test_struct::constVar' even though the header file with constVar definition is included only once and I use the --allow-multiple-definition (just in case)... Please help! – Ryan May 27 '11 at 07:13
  • Nevermind, actually... I think the root of this problem is that constVar is not a public member in my case. I wonder why gcc thinks everything is fine though... – Ryan May 27 '11 at 07:19
  • I ended up relocating static const members outside of class scope in order to avoid GCC 4.2.1 linker problems... – Ryan May 27 '11 at 13:32