0

Somewhere in a .h file:

namespace gpBenchAPI {
 struct AbstractConsts {
  public:
    static const uint8_t STATUS_TAG_NOT_FOUND = 2;
 }; //AbstractConsts
}

Somewhere in another .h file:

class MyClass{
private:
     void method();
     QMap<uint8_t, QString> errorCodes;
};

Somewhere in a .cpp file:

   void MyClass::method(){
       using namespace gpBenchAPI;
       errorCodes.insert(AbstractConsts::STATUS_TAG_NOT_FOUND, "A-1");
   }

This code is rising a linking error:

  undefined reference to `gpBenchAPI::AbstractConsts::STATUS_TAG_NOT_FOUND'

but this code compiles and links:

 switch (status) {
    case AbstractConsts::STATUS_TAG_HUNTING:
        return 0;
  }

If I do :

 errorCodes.insert(2, "A-1");

no problem. Why?

Added: If, like in the suggested dupplicated question's answer, I initialize the const in a cpp file like this:

const uint8_t AbstractConsts::STATUS_TAG_NOT_FOUND = 2;

and remove the initialization from the .h file, the "switch" does not compile anymore:

The value of 'gpBenchAPI::AbstractConsts::STATUS_TAG_NOT_FOUND' is not usable in a constant expression
     case AbstractConsts::STATUS_TAG_NOT_FOUND:
                          ^
Julien
  • 846
  • 1
  • 7
  • 19

0 Answers0