如何在我的结构中声明一个类?

时间:2015-11-22 17:10:29

标签: c++

这是我的.hpp

class playerBat
{
    public:
        playerBat(int x, int y);
        sf::RectangleShape *getShape();
        void setShape(sf::RectangleShape *Shape);
    private:
        sf::RectangleShape shapeBat;
};

typedef struct s_game_elements
{
    playerBat playerOne(int, int);
    playerBat playerTwo(int, int);
} t_game_elements;

这就是我初始化它的方式:

t_game_elements gameElements;

playerBat playerBatOne(0, 200);
playerBat playerBatTwo(790, 200);

gameElements.playerOne = playerBatOne;
gameElements.playerTwo = playerBatTwo;

但是编译器说:

src/main.cpp:14:18: error: reference to non-static member function must be called gameElements.playerOne = playerBatOne;

我做错了什么? 对不起,如果这是非常明显的事情,但我最近开始学习CPP,我从来没有在C中出现过这个错误。

1 个答案:

答案 0 :(得分:2)

看来你的意思是以下

typedef struct  s_game_elements
{
    playerBat   playerOne;
    playerBat   playerTwo;
}

否则在这个typedef声明中,struture有两个函数,而不是playerBat类型的数据成员

typedef struct  s_game_elements
{
    playerBat   playerOne(int, int);
    playerBat   playerTwo(int, int);
}

考虑到存在拼写错误

t_game_elements gameElements;
^^^

早期typedef声明中的typedef名称看起来像

s_game_elements
^^^