如何设置枚举类型提示的默认值,我尝试将其设置为 0 或 1,或者什么都不设置,但出现相同的错误?
enum tip {
pop,
rap,
rock
};
class Pesna{
private:
char *ime;
int vremetraenje;
tip tip1;
public:
//constructor
Pesna(char *i = "NULL", int min = 0, tip t){
ime = new char[strlen(i) + 1];
strcpy(ime, i);
vremetraenje = min;
tip1 = t;
}
};
Run Code Online (Sandbox Code Playgroud)
您必须将其设置为以下enum值之一,例如:
Pesna(char *i = "NULL", int min = 0, tip t = pop)
// ^^^^^
Run Code Online (Sandbox Code Playgroud)
另一种技术是使用Default在enum自身中声明的值并使用该值。如果您以后改变主意,默认值应该是什么,这会更容易:
enum tip {
pop,
rap,
rock,
Default = rap, // Take care not to use default, that's a keyword
};
Pesna(char *i = "NULL", int min = 0, tip t = Default)
// ^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)