Hi everybody,

I think this question will be kind of stupid for you ... but I am fighting with since yesterday evening.

I have a class Test defined as such in the hpp file (Test.hpp)

#include <iostream>
#include <vector>

using namespace std;

class Test {
  public:
    Test();
    static const int SOMEVALUE = 1200;
    void testFunction();
  private:
    vector <int> vectorOfValues;
};

The file Test.cpp is as such :

#include <vector>
#include <iostream>

#include "Test.hpp"

using namespace std;

Test::Test() {
}

void Test::testFunction() {
  vectorOfValues.push_back(SOMEVALUE);
}

The main file is very simple :

#include <iostream>
#include "Test.hpp"

using namespace std;

int main() {
  Test t;
  t.testFunction();
   
}

When trying to compile, the compiler returns the following error :

/tmp/ccA63Hmn.o: In function `Test::testFunction()':
Test.cpp:(.text+0x45): undefined reference to `Test::SOMEVALUE'
collect2: ld returned 1 exit status

However, if modify the testFunction function (see below) I don't have this problem, meaning that there is no access problem to my SOMEVALUE constant.

void Test::testFunction() {
  cout << SOMEVALUE << endl;
}

What do you think of it?
I thank you a lot for your help!

It seems I've got the solution ... but I don't understand it.

If I use the following code for the function testFunction, it compiles. Does anyone have some clue why it behaves so strangely?

void Test::testFunction() {
  vectorOfValues.push_back((int)SOMEVALUE);
}

You are attempting to initialize the value of SOMEVALUE inside the class declaration. You can't do that. You need to declare the static variable inside the definition, then use a "global" initialization statement outside the class definition.

In test.hpp:

//<<snip>>

class Test {
  public:
   //<<snip>>
    static const int SOMEVALUE;
    //class-member declaration (note that there is no initialization)
  //<<snip>
};

In test.cpp:

//<<snip>>
const int Test::SOMEVALUE = 1200;  //"global" redeclaration and initialization

//<<snip>>

You are attempting to initialize the value of SOMEVALUE inside the class declaration. You can't do that. You need to declare the static variable inside the definition, then use a "global" initialization outside the class definition.

In test.hpp:

//<<snip>>

class Test {
  public:
   //<<snip>>
    static const int SOMEVALUE;
    //class-member declaration (note that there is no initialization)
  //<<snip>
};

In test.cpp:

//<<snip>>
const int Test::SOMEVALUE = 1200;  //"global" redeclaration and initialization

//<<snip>>

Hi Fbody,

Thanks for your answer. Your solution corresponds to what I just found on another forum (http://stackoverflow.com/questions/272900/c-undefined-reference-to-static-class-member). However, on this thread I also found another explanation to this problem.

Apparently, as you said it WAS forbidden to declare static constant in the class definition but it is not the case anymore (see http://www.devx.com/tips/Tip/5602).

The good news... is that now my problem is solved! Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.