I am trying to make a member variable in a class available as lifetime storage with the object (translation unit), so I made it static:
#include <iostream>
class A {
public:
std::string firstName;
static std::string lastName;
void assignFirstName() {
std::cout << "Enter First Name: ";
getline (std::cin, firstName);
}
};
int main()
{
A objA;
objA.assignFirstName();
objA.lastName = "Anderson";
std::cout << objA.firstName << " " << objA.lastName << '\n';
return 0;
}
but then I got error:
undefined reference to `A::lastName[abi:cxx11]'
I don't quite understand why I am getting this error. I searched online and saw one post talking about adding inline and therefore I gave it a try, surprisingly it worked but with a warning.
inline static std::string lastName;
The warning:
warning: inline variables are only available with -std=c++1z or -std=gnu++1z
inline static std::string lastName;
I am not quite sure what's going on with the concepts behind all these. Could anyone demystify these? or any recommended explanations about these?
Thank you.