尝试从本地 class 方法访问属性时出现编译错误
Compilation error when trying to access an attribute from a local class method
我试图通过 "inner"(本地)class 的方法访问 "outer" class 的属性,但我失败了。
编译失败
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;
}
编译错误:
9:21: error: invalid use of non-static data member 'outer::name'
21:53: error: from this location
我真的不希望 name
成为静态成员,但我真的不介意 outer
是一个单身人士。所以我尝试了 static std::string name;
并得到了
编译错误:
/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
你能帮帮我吗?
你的问题出在你的hello()
功能上。 name
超出范围。它不属于您的 inner
class。不幸的是,你的内部 class 将无法看到你的外部 class 及其成员,因此:
void outer::inner::hello(){
std::cout << "Hello " << name << "\n";
}
将产生一个错误,告诉您 name
找不到。
您可以执行以下操作:
#include <iostream>
#include <string>
class outer
{
public:
static std::string name;
class inner
{
public:
void hello();
};
void dostuff();
};
std::string outer::name = ""; // This is key. You need to instantiate outer's name somewhere.
void outer::inner::hello(){std::cout << "Hello " << outer::name << "\n";}
void outer::dostuff(){inner instanceInner; instanceInner.hello();}
int main()
{
outer instanceOuter;
instanceOuter.name = std::string("Alice");
instanceOuter.dostuff();
return 0;
}
输出:
Hello Alice
重复(稍作修改)我在另一个答案中提到的内容 similar recent SO question:
A C++ nested class does not share data with its outer class -- if there
were the case, each instance of the inner class would be in a
one-to-one relationship with a corresponding instance of an outer
class and would thus add no extra functionality. Instead, the main
purposes of nested classes are:
- Namespace meaning:
outer::inner
is more meaningful than outer_inner
- Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
- Maybe others that I'm not aware of
这是一个关于 when/why 在 C++ 中使用嵌套 classes 的热门参考问题:Why would one use nested classes in C++?
在你的情况下,inner
class 不能引用数据 name
没有 a) outer
class 的特定实例,其name
将被访问,或者 b) 对 outer
通用的静态 name
。第一个解决方案需要 inner
class 引用 outer
class:
inner (const outer& _o) : o(_o) {}
其中 o
是类型 const outer&
的私有成员。然后写hello
函数
void outer::inner::hello(){std::cout << "Hello " << o.name << "\n";}
或者,如果你想使name
静态,它是一个未定义的引用,因为你必须把
std::string outer::name = "bla";
在某个编译单元中,否则未定义静态成员。
但是,无论哪种情况,我都担心您误用了嵌套 class。它在您的代码中有什么作用?为什么数据和成员函数必须在外部和嵌套之间分开class?您确定 class 不会更好地满足您的目的吗?
我试图通过 "inner"(本地)class 的方法访问 "outer" class 的属性,但我失败了。
编译失败
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;
}
编译错误:
9:21: error: invalid use of non-static data member 'outer::name'
21:53: error: from this location
我真的不希望 name
成为静态成员,但我真的不介意 outer
是一个单身人士。所以我尝试了 static std::string name;
并得到了
编译错误:
/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
你能帮帮我吗?
你的问题出在你的hello()
功能上。 name
超出范围。它不属于您的 inner
class。不幸的是,你的内部 class 将无法看到你的外部 class 及其成员,因此:
void outer::inner::hello(){
std::cout << "Hello " << name << "\n";
}
将产生一个错误,告诉您 name
找不到。
您可以执行以下操作:
#include <iostream>
#include <string>
class outer
{
public:
static std::string name;
class inner
{
public:
void hello();
};
void dostuff();
};
std::string outer::name = ""; // This is key. You need to instantiate outer's name somewhere.
void outer::inner::hello(){std::cout << "Hello " << outer::name << "\n";}
void outer::dostuff(){inner instanceInner; instanceInner.hello();}
int main()
{
outer instanceOuter;
instanceOuter.name = std::string("Alice");
instanceOuter.dostuff();
return 0;
}
输出:
Hello Alice
重复(稍作修改)我在另一个答案中提到的内容 similar recent SO question:
A C++ nested class does not share data with its outer class -- if there were the case, each instance of the inner class would be in a one-to-one relationship with a corresponding instance of an outer class and would thus add no extra functionality. Instead, the main purposes of nested classes are:
- Namespace meaning:
outer::inner
is more meaningful thanouter_inner
- Depending on the C++ version standard, a nested class has extra visibility of the member objects of instances of the outer class
- Maybe others that I'm not aware of
这是一个关于 when/why 在 C++ 中使用嵌套 classes 的热门参考问题:Why would one use nested classes in C++?
在你的情况下,inner
class 不能引用数据 name
没有 a) outer
class 的特定实例,其name
将被访问,或者 b) 对 outer
通用的静态 name
。第一个解决方案需要 inner
class 引用 outer
class:
inner (const outer& _o) : o(_o) {}
其中 o
是类型 const outer&
的私有成员。然后写hello
函数
void outer::inner::hello(){std::cout << "Hello " << o.name << "\n";}
或者,如果你想使name
静态,它是一个未定义的引用,因为你必须把
std::string outer::name = "bla";
在某个编译单元中,否则未定义静态成员。
但是,无论哪种情况,我都担心您误用了嵌套 class。它在您的代码中有什么作用?为什么数据和成员函数必须在外部和嵌套之间分开class?您确定 class 不会更好地满足您的目的吗?