c++ - 将枚举作为函数参数传递

标签 c++ c++11

这是一个说明我的问题的简化示例。以下是我的:error C2065: 'State' : undeclared identifier

我的类.h

class MyClass
{
    enum class State { IDLE, WALK_RIGHT, WALK_LEFT };
    void SetState(State);
}

我的类.cpp

#include "myclass.h"
void SetState(State state)
{
    //...
}

知道这里出了什么问题吗?提前致谢!

最佳答案

这不是您在类定义之外定义成员函数的方式。你需要

void MyClass::SetState(State state){
//   ^^^^^^^^^
}

编译器在看到 MyClass::SetState 并意识到这是一个成员函数定义后,就会知道在 MyClass 中寻找 State ,因此您不必在此处编写 MyClass::State

关于c++ - 将枚举作为函数参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652901/

相关文章:

c++ - << C++ 中的运算符?

c++ - C++ 中全局函数中的静态常量

c++ - 结合 libsndfile 和 RtAudio?

c++ - kdevelop 带参数执行项目

c++ - 模拟不使用元组的自定义映射的 std::map 迭代器

c++ - 常数整数提升规则?

c++ - 与 Windows 相比,将数组写入文件的相同代码在 Linux 上少写了 1 行

c++ - 是否定义了函数参数的内部执行顺序?

c++ - std::tolower 和 Visual Studio 2013

c++ - reinterpret_cast(或任何强制转换)可以将 xvalues 转换为 lvalues 吗?