0

I have a .cpp file that looks something like this:

//other code
namespace {
    class C1;
    class C2;
    class C2{
         public: static int counter;
         //member functions here
    };
    class C1{
         //other code
         C2::counter = 10;
    };
}

When I run 'make' I get the following error:

relocation R_386_GOTOFF against undefined symbol '(anonymous namespace)::C2::counter' can not be used when making a shared object...

Am I missing something simple here? Shouldn't the static int be available for class C1 to change it? Also, I am developing this as a part of the Clang library's. Also, I can share the Makefile if that helps.

Nathaniel Wendt
  • 1,194
  • 4
  • 23
  • 49
  • The other post was helpful, thank you. I looked for similar posts for quite some time before posting this, and you found it quite quickly. – Nathaniel Wendt Sep 17 '13 at 23:17
  • here's a pro-tip: search inside the [tag:c++-faq] tag using `[c++-faq]` in the search box. My query was [`[c++-faq] undefined`](http://stackoverflow.com/search?q=%5Bc%2B%2B-faq%5D+undefined) – sehe Sep 17 '13 at 23:32
  • @thepristinedesign: Just curious, why do you forward declare C1 and C2? – thokra Sep 17 '13 at 23:53

1 Answers1

1

You have missed out on providing the definition of you static variable. This definition must occur outside the class and only one definition is allowed. Usual way to do this is to provide the definition in the implementation file.

Because you are directly using it, without providing any definition for it, you are getting the error.

NotAgain
  • 1,927
  • 3
  • 26
  • 42