0

I'm trying to define some static constant strings in C++ and reference them from different files.

Here's how I have the information set up this far:

structClass.h

namespace test {
     typedef struct customstructure{
          static const std::string stringA;
      } customstructure;
}

structClass.cpp

namespace test {
     static const std::string customstructure::stringA = "This is String A";
}

Now I'm wondering how I would call this in a third file?

execute.cpp

void printStringA(){
    printf("stringA is: %s", test::customstructure::stringA.c_str());
}

gives me a compile error that says:

undefined reference to 'test::customstructure::stringA'

pledgehollywood
  • 109
  • 1
  • 1
  • 10

1 Answers1

0

In this code:

namespace test {
     static const std::string customstructure::stringA = "This is String A";
}

remove the word static. In fact it is an error to have this, your compiler should give a more useful error message (although I suppose 'undefined reference' meets the requirements of "a diagnostic").

Standard reference: [class.static.data]#5 says that static data members have external linkage, however using the keyword static in the definition would specify internal linkage.

M.M
  • 138,810
  • 21
  • 208
  • 365