我正在尝试创建一个包含以下类型的结构的类:
struct gameObject
{
int row; // row position of the object
int column; // column position of the object
bool isFound; // flag to indicate if the object has been found (or is dead, in the case of the monster)
bool isVisible; // flag to indicate if the object can be seen on the board -add in week 4
};
struct playerObject
{
bool alive; // flag to indicate if the player is alive or dead
bool hasWeapon; // flag to indicate if the player has the weapon
bool hasTreasure; // flag to indicate if the player has the treasure
bool hasTorch; // flag to indicate if the player has the torch -add in week 4
bool hasNoisemaker; // flag to indicate if the player has the noisemaker -add in week 4
bool hasKey;
gameObject position; // variables for row, column and visibility
};
我这样做是为了将我所有的“游戏片段”及其数据都放在一个对象下,以期有助于提高可读性。
我要声明的类如下:
class pieces{//the class for the game and player objects
public:
pieces();
~pieces();
gameObjectType hold = EMPTY; // Holds objects under the monster<bug fix>
// </WK6>
playerObject player = { true, false, false, false, false, false, { -1, -1, false, true } }; // the player
gameObject treasure ={ -1, -1, false, true }; // the treasure
gameObject monster = { -1, -1, false, true }; // the monster
gameObject weapon = { -1, -1, false, true }; // the weapon
gameObject torch = { -1, -1, false, true }; // the torch
gameObject noisemaker = { -1, -1, false, true }; // the noisemaker
gameObject key = { -1, -1, false, true };
gameObject caveExit = { -1, -1, false, true }; // the cave exit
};
问题是我不断收到C2661错误。对于我所有的gameObject声明,它都说
error C2661: 'gameObject::gameObject' : no overloaded function takes 4 arguments
但是,对于我的playerObject来说,除了7个参数而不是4个参数外,它表示的都是相同的。
我已经呆了几个小时,无法弄清楚发生了什么。
最佳答案
如果声明结构,则如下所示:
typedef struct gameObject {
{
int row; // row position of the object
int column; // column position of the object
bool isFound; // flag to indicate if the object has been found (or is dead, in the case of the monster)
bool isVisible; // flag to indicate if the object can be seen on the board -add in week 4
} gameObject;
然后,我希望这一切都会奏效。注意'}'之后的gameObject
关于c++ - C++错误C2661:没有重载函数接受x个参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41176827/