我想用3个参数实例化一个对象的Bug Bug,其中一个是枚举器。这里是我的班级:如何用C++中的枚举参数实例化对象?
class Bug
{
private:
int Id;
string description;
enum severity { low, medium, severe} s;
public:
Bug(void);
Bug(int id, string descr, severity x)
:Id(id), description(descr), s(x)
{}
void printDetails()
{
cout<< "Severity level:" <<s<< " Description: " <<description<<" ID= "
<<Id<< endl;
}
~Bug(void);
};
这是我的main.cpp:
#include "Bug.h"
int main(){
Bug bg(3,"a", low);//Error message: identifier "low" is undefined
return 0;
}
,当我加入此行的主要
enum severity { low, medium, severe};
错误信息已更改为这样:
Bug bg(3,"a", low);//Error message: no instance of constructor "Bug::bug" matches the argument list
任何想法如何做到这一点?
尝试'错误:: low',或'错误::严重:: low'。 – apalomer
将枚举定义移动到公共部分。 main()不能看到它,因为它是女贞。 –