枚举使用问题
Issue with Enum usage
我对 Enum 有疑问(我猜测它的用法):我在 class 中声明了一个 Enum,我正试图在它的子 class 中使用它。这里是(抽象的)superclass:
//Function.h
class Function {
public:
enum FunctionType {
constant,
variable,
sum,
mul
};
...
virtual FunctionType getType();
};
这里是它的子class之一:
//Const.h
#include "Function.h"
#ifndef CONST_H_
#define CONST_H_
class Const:public Function {
....
public:
Const(double x);
....
Function::FunctionType getType();
....
~Const();
};
#endif /* CONST_H_ */
及其实现:
//Const.cpp
#include "Const.h"
#include "Function.h"
Function::FunctionType Const::getType(){
return Function::FunctionType::constant;
}
....
编译器抛出以下错误:
error: ‘Function::FunctionType’ is not a class or namespace
return Function::FunctionType::constant;
^
我无法找出出现此错误的原因,以及这段听起来很简单(当然不是)的代码有什么问题。
尝试:
return Function::constant;
而不是:
return Function::FunctionType::constant;
要限定枚举值,不要使用枚举类型名称:
enum enum_t { ENUM_VALUE };
auto e = ENUM_VALUE; // no enum_t::
对于您的情况,解决方案是:
struct Function {
enum FunctionType {
constant
};
virtual FunctionType getType();
};
struct Const : Function {
Function::FunctionType getType() { return Function::constant; }
};
事实上 Function::FunctionType::constant
是一个错误,因为枚举是声明常量的 C 特性。从cppreference.com page on the enum可以看出:
Unscoped enumeration
enum name { enumerator = constexpr , enumerator = constexpr , ... }
(1)
1) Declares an unscoped enumeration type whose underlying type is not fixed (in this case, the underlying type is either int or, if not all enumerator values can be represented as int, an implementation-defined larger integral type that can represent all enumerator values. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0).
如果您想使用强类型、范围枚举,请参阅 答案和 enum class
。
100% 正确地解释了您遇到的错误。
为了添加更广泛的上下文,从 C++11 开始以 范围枚举的形式支持您最初打算使用的语法。这些是隐式转换的枚举to/from 整数不可用,枚举数的名称在枚举范围内。它们由语法 enum class
.
引入
这与无范围枚举(仅使用 enum
得到的结果)形成对比,后者不存在此类范围规则。枚举名称根本不引入作用域,所以在作用域解析运算符::
的左侧使用它是错误的。
您可以像这样在代码中使用范围枚举:
class Function {
public:
enum class FunctionType {
constant,
variable,
sum,
mul
};
...
virtual FunctionType getType();
};
Function::FunctionType Const::getType(){
return Function::FunctionType::constant;
}
请注意,在从 Function
派生的 class 中不需要将 FunctionType::constant
限定为 Function::FunctionType::constant
;对从 Function
继承的所有成员的访问是隐式的。
我对 Enum 有疑问(我猜测它的用法):我在 class 中声明了一个 Enum,我正试图在它的子 class 中使用它。这里是(抽象的)superclass:
//Function.h
class Function {
public:
enum FunctionType {
constant,
variable,
sum,
mul
};
...
virtual FunctionType getType();
};
这里是它的子class之一:
//Const.h
#include "Function.h"
#ifndef CONST_H_
#define CONST_H_
class Const:public Function {
....
public:
Const(double x);
....
Function::FunctionType getType();
....
~Const();
};
#endif /* CONST_H_ */
及其实现:
//Const.cpp
#include "Const.h"
#include "Function.h"
Function::FunctionType Const::getType(){
return Function::FunctionType::constant;
}
....
编译器抛出以下错误:
error: ‘Function::FunctionType’ is not a class or namespace
return Function::FunctionType::constant;
^
我无法找出出现此错误的原因,以及这段听起来很简单(当然不是)的代码有什么问题。
尝试:
return Function::constant;
而不是:
return Function::FunctionType::constant;
要限定枚举值,不要使用枚举类型名称:
enum enum_t { ENUM_VALUE };
auto e = ENUM_VALUE; // no enum_t::
对于您的情况,解决方案是:
struct Function {
enum FunctionType {
constant
};
virtual FunctionType getType();
};
struct Const : Function {
Function::FunctionType getType() { return Function::constant; }
};
事实上 Function::FunctionType::constant
是一个错误,因为枚举是声明常量的 C 特性。从cppreference.com page on the enum可以看出:
Unscoped enumeration
enum name { enumerator = constexpr , enumerator = constexpr , ... }
(1)
1) Declares an unscoped enumeration type whose underlying type is not fixed (in this case, the underlying type is either int or, if not all enumerator values can be represented as int, an implementation-defined larger integral type that can represent all enumerator values. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0).
如果您想使用强类型、范围枚举,请参阅 enum class
。
为了添加更广泛的上下文,从 C++11 开始以 范围枚举的形式支持您最初打算使用的语法。这些是隐式转换的枚举to/from 整数不可用,枚举数的名称在枚举范围内。它们由语法 enum class
.
这与无范围枚举(仅使用 enum
得到的结果)形成对比,后者不存在此类范围规则。枚举名称根本不引入作用域,所以在作用域解析运算符::
的左侧使用它是错误的。
您可以像这样在代码中使用范围枚举:
class Function {
public:
enum class FunctionType {
constant,
variable,
sum,
mul
};
...
virtual FunctionType getType();
};
Function::FunctionType Const::getType(){
return Function::FunctionType::constant;
}
请注意,在从 Function
派生的 class 中不需要将 FunctionType::constant
限定为 Function::FunctionType::constant
;对从 Function
继承的所有成员的访问是隐式的。