如何在源文件中实现嵌套 Class 构造函数
How to implement Nested Class Constructor in Source file
我在主 class 中有一个嵌套的 class 单元格。
我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 文件中实现嵌套的 class 构造函数
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::Something::Cell 是正确的,但我也不认为那是真的。
你快了。就这么简单:
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)
{}
你需要让你的内classpublic,方法set_Position_Letter不能是静态的,因为char position_Letter
不是静态的(这里是header):
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;
}
我在主 class 中有一个嵌套的 class 单元格。 我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 文件中实现嵌套的 class 构造函数
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::Something::Cell 是正确的,但我也不认为那是真的。
你快了。就这么简单:
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)
{}
你需要让你的内classpublic,方法set_Position_Letter不能是静态的,因为char position_Letter
不是静态的(这里是header):
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;
}