2017-10-10 206 views
2

我在我的主類中有一個名爲cell的嵌套類。 I C如何在源文件中實現嵌套類構造函數

class Something{ 
    class Cell 
    { 
    public: 
     int get_row_Number(); 
     void set_row_Number(int set); 

     char get_position_Letter(); 
     static void set_position_Letter(char set); 

     void set_whohasit(char set); 
     char get_whohasit(); 

     Cell(int row,char letter,char whohasit); 

    private: 
     char position_Letter; 
     int row_Number; 
     char whohasit; 
    }; 
}; 

我想實現.cpp文件嵌套類的構造函數

Something::Cell Cell(int row,char letter,char whohasit){ 
    Something::Cell::set_position_Letter(letter); 
    Something::Cell::set_row_Number(row); 
    Something::Cell::set_whohasit(whohasit); 
} 

但它是錯誤的。我認爲正確的將是首先,但我不認爲這是真的。

+1

'Something :: Cell Cell(int row,...)' - >'Something :: Cell :: Cell(int row,...)'..不在'Something'中, Cell的構造函數是'Cell :: Cell'。在命名空間中,ust前綴爲單個'Something ::' – Peter

回答

2

你是差不多那裏。這是簡單的:

Something::Cell::Cell(int row,char letter,char whohasit){ 
    Something::Cell::set_position_Letter(letter); 
    Something::Cell::set_row_Number(row); 
    Something::Cell::set_whohasit(whohasit); 
} 

但實際上,我會強烈建議您使用初始化,而不是構建成員初始化,然後分配給他們:

Something::Cell::Cell(int row, char letter, char whohasit) 
    :position_Letter(letter) 
    ,row_Number(row) 
    ,whohasit(whohasit) 
{} 
+0

這個工作很好。 – paypaytr

1

你需要讓你的內部類公衆和方法set_Position_Letter不能是靜態的,因爲char position_Letter不是靜態的(這裏是頭部):

class Something 
{ 
public: 
    class Cell { 
    public: 
     int get_row_Number(); 
     void set_row_Number(int set); 

     char get_position_Letter(); 
     void set_position_Letter(char set); 

     void set_whohasit(char set); 
     char get_whohasit(); 

     Cell(int row,char letter,char whohasit); 

    private: 
     char position_Letter; 
     int row_Number; 
     char whohasit; 
    }; 
}; 

這是CPP:

Something::Cell::Cell(int row, char letter, char whohasit) { 
    set_position_Letter(letter); 
    set_row_Number(row); 
    set_whohasit(whohasit); 
} 

void Something::Cell::set_position_Letter(char set) { 
    this->position_Letter = set; 
} 

void Something::Cell::set_whohasit(char set) { 
    this->whohasit = set; 
} 

void Something::Cell::set_row_Number(int set) { 
    this->row_Number = set; 
} 
+0

謝謝,但我內心的類必須是私人的,因爲某些原因,我不會詳細說明。 – paypaytr