我想用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()不能看到它,因爲它是女貞。 –