2016-04-29 115 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); 
}