我想制作一些我粘贴在代码中的东西。 我想在 Head 类中使用 Nested 类,请查看下面的代码。 我应该怎么办?我试图在初始化列表中使用嵌套构造器,但仍然无法正常工作。有什么想法吗?
class Head{
private:
int x;
public:
Head(int x, const Nested& n){
this->x=x;
}
class Nested{
private:
int a;
int b;
public:
Nested(int a, int b){
this->a=a;
this->b=b;
}
}
最佳答案
你的意思是你有一个编译错误?你应该在使用之前定义 Nested ,如下所示:
class Head{
private:
int x;
public:
class Nested {
private:
int a;
int b;
public:
Nested(int a, int b){
this->a=a;
this->b=b;
}
};
Head(int x, const Nested& n){
this->x=x;
}
};
int main()
{
Head::Nested n(0, 0);
Head h(0, n);
}
关于c++ - 在头类构造函数中使用嵌套类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36934373/