将枚举作为参数传递给构造函数
Passing enum as parameter to a constructor
当我想将枚举值传递给默认构造函数时遇到问题。我的枚举定义如下:
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
它们存储在单独的 .h 文件中。
但是当我尝试这样做时:
chips m_doubloon(DOUBLOON);
我收到以下错误:
error: C2061: syntax error : identifier 'DOUBLOON'
默认构造函数的代码是:
chips::chips(
ENUMchipType chipType = DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0,
QObject *parent = 0) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5),
QObject(parent) {}
有人知道这段代码有什么问题吗?提前致谢!
编辑:我已经尝试将枚举作为 class 和 public 成员并从中派生筹码 class,但没有任何成功。
编辑 2:这段代码重现了 Visual Studio 2013
中的错误
#include <string>
using namespace std;
//enums.h
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
typedef enum
{
PLAYER1,
PLAYER2,
PLAYER3,
PLAYER4,
PLAYER5
} ENUMplayer;
// In chips.h
class chips
{
private:
int m_chipCountValue5;
int m_chipCountValue1;
ENUMchipType m_chipType;
public:
explicit chips(
ENUMchipType chipType = ENUMchipType::DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0);
ENUMchipType getChipType() const { return m_chipType; }
};
// Chips.cpp
chips::chips(ENUMchipType chipType, int amountValue1, int amountValue5) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5) {}
// PLayer.h
class player
{
private:
ENUMplayer m_ID;
string m_name;
public:
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
explicit player(ENUMplayer ID = PLAYER1, string name = "");
void setName(string name = "") { m_name = name; }
void setID(ENUMplayer ID) { m_ID = ID; }
string getName() const { return m_name; }
ENUMplayer getID() const { return m_ID; }
};
//player.cpp
player::player(ENUMplayer ID, string name) :
m_ID(ID),
m_name(name) {}
int main() {
return 0;
}
您需要将 DOUBLOON
作为 ENUMchipType::DOUBLOON
在classplayer
中,你应该替换
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
来自
chips m_doubloon{DOUBLOON};
chips m_victoryPoints{VICTORYPOINT};
现在您终于发布了足够多的代码,我们看到了这个
chips m_doubloon(DOUBLOON);
实际上是一个class成员声明。 Class 成员不能用 ()
初始化,只能用 =
或 {}
初始化。假设你的编译器支持 in-class 初始化(在 C++11 中引入),你应该没问题
chips m_doubloon{DOUBLOON};
^ ^
或者,您可以在构造函数的初始化列表中而不是在它们的声明中初始化成员。
当我想将枚举值传递给默认构造函数时遇到问题。我的枚举定义如下:
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
它们存储在单独的 .h 文件中。
但是当我尝试这样做时:
chips m_doubloon(DOUBLOON);
我收到以下错误:
error: C2061: syntax error : identifier 'DOUBLOON'
默认构造函数的代码是:
chips::chips(
ENUMchipType chipType = DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0,
QObject *parent = 0) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5),
QObject(parent) {}
有人知道这段代码有什么问题吗?提前致谢!
编辑:我已经尝试将枚举作为 class 和 public 成员并从中派生筹码 class,但没有任何成功。
编辑 2:这段代码重现了 Visual Studio 2013
中的错误#include <string>
using namespace std;
//enums.h
typedef enum
{
DOUBLOON,
VICTORYPOINT
} ENUMchipType;
typedef enum
{
PLAYER1,
PLAYER2,
PLAYER3,
PLAYER4,
PLAYER5
} ENUMplayer;
// In chips.h
class chips
{
private:
int m_chipCountValue5;
int m_chipCountValue1;
ENUMchipType m_chipType;
public:
explicit chips(
ENUMchipType chipType = ENUMchipType::DOUBLOON,
int amountValue1 = 0,
int amountValue5 = 0);
ENUMchipType getChipType() const { return m_chipType; }
};
// Chips.cpp
chips::chips(ENUMchipType chipType, int amountValue1, int amountValue5) :
m_chipType(chipType),
m_chipCountValue1(amountValue1),
m_chipCountValue5(amountValue5) {}
// PLayer.h
class player
{
private:
ENUMplayer m_ID;
string m_name;
public:
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
explicit player(ENUMplayer ID = PLAYER1, string name = "");
void setName(string name = "") { m_name = name; }
void setID(ENUMplayer ID) { m_ID = ID; }
string getName() const { return m_name; }
ENUMplayer getID() const { return m_ID; }
};
//player.cpp
player::player(ENUMplayer ID, string name) :
m_ID(ID),
m_name(name) {}
int main() {
return 0;
}
您需要将 DOUBLOON
作为 ENUMchipType::DOUBLOON
在classplayer
中,你应该替换
chips m_doubloon(DOUBLOON);
chips m_victoryPoints(VICTORYPOINT);
来自
chips m_doubloon{DOUBLOON};
chips m_victoryPoints{VICTORYPOINT};
现在您终于发布了足够多的代码,我们看到了这个
chips m_doubloon(DOUBLOON);
实际上是一个class成员声明。 Class 成员不能用 ()
初始化,只能用 =
或 {}
初始化。假设你的编译器支持 in-class 初始化(在 C++11 中引入),你应该没问题
chips m_doubloon{DOUBLOON};
^ ^
或者,您可以在构造函数的初始化列表中而不是在它们的声明中初始化成员。