2016-04-29 114 views
0

我想制作一些粘贴在代码中的东西。 我想在Head类中使用嵌套类,请看下面的代码。 我该怎么办?我试图在初始化列表中使用嵌套的构造函数,但仍然无法工作。有任何想法吗?在头类构造函数中使用嵌套类

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; 
     } 
    } 

}

+1

“我想在Head类中使用嵌套类”您想如何使用它?作为一个成员变量? – songyuanyao

回答

1

你的意思是你有一个编译错误?您应该在使用之前定义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); 
}