gpt4 book ai didi

c++ - 如何设置枚举类型变量的默认值?

转载 作者:行者123 更新时间:2023-11-30 01:09:58 27 4
gpt4 key购买 nike

如何设置枚举类型提示的默认值,我尝试将其设置为 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;
}

};

最佳答案

您必须将其设置为 enum 值之一,例如:

Pesna(char *i = "NULL", int min = 0, tip t = pop)
// ^^^^^

另一种技术是使用在 enum 本身中声明的 Default 值并使用该值。如果您稍后改变主意,默认值应该是什么,这会更容易:

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)
// ^^^^^^^^^

关于c++ - 如何设置枚举类型变量的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39146877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com