c++ public 成员仅在构造函数具有参数时才可访问
c++ public members only accessible if constructor has a parameter
所以我创建了这个 class,其中包含一些 public 成员,它们是布尔值,但是,当我实际尝试使用默认构造函数(无参数)创建对象时,我无法访问这些成员。但是,如果我用参数 (unsigned int) 声明构造函数,我就可以访问成员。有人知道为什么吗?
以下为class:
class MoveManagement {
private:
Player * currentPlayer;
sf::View* currentView;
int lambda_x = 0;
int lambda_y = 0;
public:
bool m_up;
bool m_down;
bool m_left;
bool m_right;
MoveManagement() {
m_up = false;
m_down = false;
m_left = false;
m_right = false;
}
void getNextView(Player* player_, sf::View* view_) {
currentPlayer = player_;
currentView = view_;
if (m_up) {
--lambda_y;
}
if (m_down) {
++lambda_y;
}
if (m_left) {
--lambda_x;
}
if (m_right) {
++lambda_x;
}
currentPlayer->playerCharacter->m_position.x = currentPlayer->playerCharacter->m_position.x + lambda_x;
currentPlayer->playerCharacter->m_position.y = currentPlayer->playerCharacter->m_position.y + lambda_y;
currentView->move(lambda_x, lambda_y);
lambda_x = 0;
lambda_y = 0;
}
};
我这样创建一个新对象:
MoveManagement move();
如果我尝试访问任何成员,我会收到一条错误消息,提示 "expression must have class type"。
谢谢。
MoveManagement move();
声明了一个函数。请改用 MoveManagement move;
。
所以我创建了这个 class,其中包含一些 public 成员,它们是布尔值,但是,当我实际尝试使用默认构造函数(无参数)创建对象时,我无法访问这些成员。但是,如果我用参数 (unsigned int) 声明构造函数,我就可以访问成员。有人知道为什么吗?
以下为class:
class MoveManagement {
private:
Player * currentPlayer;
sf::View* currentView;
int lambda_x = 0;
int lambda_y = 0;
public:
bool m_up;
bool m_down;
bool m_left;
bool m_right;
MoveManagement() {
m_up = false;
m_down = false;
m_left = false;
m_right = false;
}
void getNextView(Player* player_, sf::View* view_) {
currentPlayer = player_;
currentView = view_;
if (m_up) {
--lambda_y;
}
if (m_down) {
++lambda_y;
}
if (m_left) {
--lambda_x;
}
if (m_right) {
++lambda_x;
}
currentPlayer->playerCharacter->m_position.x = currentPlayer->playerCharacter->m_position.x + lambda_x;
currentPlayer->playerCharacter->m_position.y = currentPlayer->playerCharacter->m_position.y + lambda_y;
currentView->move(lambda_x, lambda_y);
lambda_x = 0;
lambda_y = 0;
}
};
我这样创建一个新对象:
MoveManagement move();
如果我尝试访问任何成员,我会收到一条错误消息,提示 "expression must have class type"。
谢谢。
MoveManagement move();
声明了一个函数。请改用 MoveManagement move;
。