I am trying to access the attribute of an "outer" class from a method of an "inner" (local) class but I fail.
This fails to compile
class outer
{
public:
std::string name;
class inner
{
public:
void hello();
};
void dostuff();
};
void outer::inner::hello(){std::cout << "Hello " << name << "\n";}
void outer::dostuff(){inner instanceInner; instanceInner.hello();}
int main()
{
outer instanceOuter;
instanceOuter.name = std::string("Alice");
instanceOuter.dostuff();
return 0;
}
Compilation Error:
9:21: error: invalid use of non-static data member 'outer::name'
21:53: error: from this location
I don't really want name
to be a static member but I don't really mind as for my particular purpose outer
is a singleton. So I tried with static std::string name;
and got
Compilation Error:
/tmp/ccqVKxC4.o: In function `outer::inner::hello()':
:(.text+0x4b): undefined reference to `outer::name'
/tmp/ccqVKxC4.o: In function `main':
:(.text.startup+0x1f): undefined reference to `outer::name'
collect2: error: ld returned 1 exit status
Can you help me out?