c++ - 在头类构造函数中使用嵌套类

标签 c++ class constructor nested

我想制作一些我粘贴在代码中的东西。 我想在 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/

相关文章:

c++ - 自 lambda 进入 C++ 以来, `Callback` 接口(interface)是否已过时?

c++ - 可以在 C++ 模板中强制使用连续的分配边界吗?

c++ - Xcode 无法识别 "class"这个词

c - 使用 microchip xc8 编译器声明一个类

java - 没有new的对象实例?

c++ - 当 `health` 不能高于 `maxHealth` 时,如何在一个对象中定义 `healt` 和 `maxHealth`?

c++ - 如何最好地快速填充 vector ?

C++函数返回函数

python - 在 pandas、OOP 类和字典之间进行选择 (Python)

c# - 抽象类中 'public'构造函数的相关性