c ++公共成员仅在构造函数具有参数的情况下才能访问

时间:2018-06-28 10:18:31

标签: c++ most-vexing-parse

因此,我创建了一个包含一些布尔型公共成员的类,但是,当我实际尝试使用默认构造函数(无参数)创建对象时,无法访问这些成员。但是,如果我使用参数(无符号int)声明构造函数,则可以访问成员。有人知道为什么是这样吗?

以下是课程:

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();

如果我尝试访问任何成员,都会收到一条错误消息,提示“表达式必须具有类类型”。

谢谢。

1 个答案:

答案 0 :(得分:0)

MoveManagement move();声明一个函数。请改用MoveManagement move;