Search code examples
c++most-vexing-parse

c++ public members only accessible if constructor has a parameter


so I've created this class which contains some public members which are booleans, however, when I actually try to create an object with the default constructor (no parameters) I cannot access these members. However, if I declare the constructor with a parameter (unsigned int), I can access the members. Does anyone know why this is?

The following is the 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;
    }
};

I create a new object like this:

MoveManagement move();

If I try to access any of the members I get an error saying the "expression must have class type".

Thanks.


Solution

  • MoveManagement move(); declares a function. Use MoveManagement move; instead.