c++ - 编译错误 - 类中的枚举

标签 c++ enums

我有一个编译错误,我不知道为什么。 我在 .h 文件和 .cpp 文件上有枚举声明,假设在 stringToEnum() 函数中使用它

这是.cpp文件

#include "A.h"

A::A(void)
{
}

A::~A(void)
{
}       

values A::stringToEnum (string inputString) {
    if (inputString == "string1") return val1;
    if (inputString == "string2") return val2;
}

这是头文件

class A
{
public:

    A(void);
    ~A(void);


private:
    enum values{
        val1,
        val2,
        val3,
        val4
    };
    values stringToEnum (string inputString);
};

这是我得到的错误:

1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2143: syntax error : missing ';' before 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2556: 'int A::stringToEnum(std::string)' : overloaded function differs only by return type from 'A::values A::stringToEnum(std::string)'
1>          c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2371: 'A::stringToEnum' : redefinition; different basic types
1>          c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'

我很乐意提供一些指导。

谢谢

最佳答案

由于 values 包含在 A 中,您需要限定名称:

A::values A::stringToEnum (string inputString) {
//...

关于c++ - 编译错误 - 类中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29793284/

相关文章:

c++ - 覆盖另一种方法使用的父类(super class)回调函数[C++]

asp.net-mvc - 枚举的自定义编辑器模板 - 如何在创建 View 中处理 null?

c# - 使用来自其他库的值的 Swig 枚举

json - 如何使用 Enum 类型的字段执行 Typesafe JSON?

c++ - 如何制作透明代理模型: QAbstractProxyModel?

c++ - fetch_add with acq_rel 内存顺序

c++ - CMake 错误 : Could not create named generator Visual Studio 15 2017 Win64-T

c++ - MSVC 枚举调试

c# - 为什么枚举权限经常有 0、1、2、4 值?

c++ - 为什么这段代码有 C2784 "could not deduce template argument"错误