【问题标题】:c++ enum parameter gives identifier errorc++枚举参数给出标识符错误
【发布时间】:2016-03-17 18:21:31
【问题描述】:

所以,我有一个名为“Player”的类,在头文件中我有这个:

class Player
{
public:
    void move(Player::Direction direction);

private:
    enum Direction { LEFT, RIGHT, UP, DOWN };
};

在 cpp 文件中我有这个:

void Player::move(Player::Direction direction)
{

}

现在我的问题是,intellisense 说标题中的类内没有方向这样的成员,但在 cpp 文件中它说它是有效的。编译时出现错误:“error C2061: syntax error : identifier 'Direction'”

【问题讨论】:

  • 你的代码能编译吗? Intellisense 在索引器通过之前是不可靠的。
  • 只需将枚举声明移到函数声明上方即可。这是c++该死的! (不生气)
  • 我刚刚对此进行了测试,如果您将 Enum 移到类的顶部,则可以完美运行。
  • 如果 move 是公开的,那么枚举也应该是公开的,以便调用代码可以使用它。

标签: c++ enums


【解决方案1】:

C++ 中的一般规则是,必须先看到事物的声明,然后才能使用该事物。

交换声明。 (另外,Player:: 是多余的。)

class Player
{
private:
    enum Direction { LEFT, RIGHT, UP, DOWN };

public:
    void move(Player::Direction direction);    
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多