简体   繁体   English

为什么我的枚举值在用作参数时“未定义”?

[英]Why is my enum value “undefined” when using it as a parameter?

A simplified model of my problem is below.我的问题的简化 model 如下。 All I'm trying to do is use the DegreeChecker() function in main().我要做的就是在 main() 中使用 DegreeChecker() function。 The DegreeChecker() function takes an enum, 'DegreeProgram', as its type. DegreeChecker() function 将枚举“DegreeProgram”作为其类型。 This enum is defined in its own class, which subsequent classes inherit.此枚举在其自己的 class 中定义,后续类继承该枚举。 But the problem is that the function in main() keeps treating any enum parameter as "undefined".但问题是 main() 中的 function 一直将任何枚举参数视为“未定义”。 Why is this happening and how can I resolve this?为什么会发生这种情况,我该如何解决?

#include <iostream>
using namespace std;

class Degree {
public:
    enum DegreeProgram { SECURITY, NETWORK, SOFTWARE };

};

class UseEnum : Degree {
public:
    bool DegreeChecker(DegreeProgram degreeProgram);
};

bool UseEnum::DegreeChecker(DegreeProgram degreeProgram) {

    if (degreeProgram == SOFTWARE) {
        return true;
    }

}



int main()
{
    UseEnum NewDegree;

    NewDegree.DegreeChecker(SOFTWARE); **//error: identifier "SOFTWARE" is undefined -- why?** 
    
    return 0;
}

The symbol is scoped to the Degree class so you will need to specify that in order to use it:该符号的范围为Degree度数,因此您需要指定它才能使用它:

NewDegree.DegreeChecker(Degree::SOFTWARE);

The enum is a member of the Degree class, so it needs to be qualified.枚举是Degree class的成员,所以需要合格。

Try尝试

NewDegree.DegreeChecker(Degree::SOFTWARE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在课堂上使用枚举会是公开的,为什么? - When using an enum in a class would it be public and why? 在函数中使用枚举参数 - Using enum parameter in functions 在基类中定义枚举时,qml中的Q_ENUM未定义值 - Q_ENUM undefined value in qml when enum is defined in base class 为什么我程序中的一个枚举值具有奇怪的值131075? - why one of the enum's in my program has strange value 131075? 为什么我的(C++)编译器在使用 std::endl 时想要实例化我的参数包 class? - Why does my (C++) compiler want to instantiate my parameter pack class when using std::endl? 使用枚举类的未定义符号 - Undefined symbol using enum class 在unordered_map位置中使用向量构造器时,为什么只使用参数的最后一个值? - Why a vector constructor takes only last value of the parameter when using in unordered_map emplace? 为什么使用int i的值; 未定义的行为,但使用rand()的值不是? - Why is using the value of int i; undefined behaviour but using the value of rand() is not? 从枚举类值模板参数中推断枚举类类型? - Infer enum class type from enum class value template parameter? 是否可以将非枚举值作为枚举函数参数传递? - Is it possible to pass a non-enum value as an enum function parameter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM