I am trying to store information about a class with a template struct with static members during runtime, however I cannot get or set the static member without an error.
Why doesn’t this code work?
template <typename T>
struct InfoHolder
{
static const char* name;
};
int main()
{
InfoHolder<int>::name = "This is an integer";
cout << InfoHolder<int>::name << endl;
return 0;
}
If you are getting a linker error. You have to define the
namevariable somewhere. (declaring it in a class doesn’t allocate it anywhere, cause it is not part of an object)write this after the class and before main:
It is a small inconvenience of static data members.