返回枚举值

时间:2019-03-31 12:56:58

标签: c++ enums

我已经编写了枚举类Repetition,现在我想在函数中返回这些枚举值之一。 但是我不知道该怎么做

枚举类:

#ifndef REPETITION_H_
#define REPETITION_H_

enum Repetition{
    none, weekly, monthly, yearly
 };

#endif /* REPETITION_H_ */

这是我想返回“每周”值的方法

Repetition getRepetition() {
        return "weekly";
    }

1 个答案:

答案 0 :(得分:1)

"weekly"const char[7]。类型错误。

您想要

return Repetition::weekly;

或者只是

return weekly;

自C ++ 11起,第一种语法适用于作用域枚举(enum class)和旧式枚举。第二种语法仅适用于旧式枚举,但也适用于C ++ 11以前的代码。