JustSomeGuy wrote:
Victor Bazarov wrote:
JustSomeGuy wrote:
I have a class that has a public data member.
If however I declare that data member as being static, then the linker
fails and says the symbol is undefined.
Must static data members be private?
No. But static data members have to be _defined_ at the namespace level.
V
Ok I think I see...
so if I have a class
class myclass
{
public:
static int x;
};
main()
{
myclass c;
cout << std::c.x;
}
Is that correct?
No. First of all, myclass::x is still not defined anywhere. Second,
what's "std::c.x"? Why is 'c' prefixed with 'std::'? Your object is
not in the 'std' namespace, is it? Third, 'cout' is undefined. Did
you forget to include <iostream>, maybe? Fourth, 'main' is a function,
every function needs a return value type: "int main()".
To define myclass::x you need to add this line:
int myclass::x;
somewhere in the same namespace where 'myclass' is defined.
V