枚举类别范围-C
Enum class out of scope - C++
这是我当前的程序标题文件
#ifndef BOARD_H
#define BOARD_H
class Board {
enum class EnemyPiece {HIT, MISS, EMPTY};
enum class PlayerPiece {AIRCRAFT, BATTLESHIP, CRUISER, SUBMARINE, PATROL, EMPTY};
private:
PlayerPiece playerboard[100];
PlayerPiece enemyboard[100];
public:
Board();
void reset(PlayerPiece *a , PlayerPiece *b);
};
#endif
这是CPP文件
#include <iostream>
#include "Board.h"
using namespace std;
Board::Board()
{
reset(playerboard, enemyboard);
}
void Board::reset(PlayerPiece *year, PlayerPiece *month)
{
std::cout << static_cast<std::underlying_type<PlayerPiece>::type>(PATROL) << std::endl;
}
在成员功能中,我试图施放枚举类型在此范围内未宣布"巡逻"我做错了什么吗?应该把枚举类放在其他地方吗?
使用 enum class
时,令牌将在类型下范围范围。因此,您需要使用PlayerPiece::PATROL
而不是PATROL
。
来自C 11标准:
7.2枚举声明
...
2 ... enum-keys
enum class
和enum struct
在语义上是等效的;其中一种声明的枚举类型是A scoped Eneumeration ,其枚举 scopeped枚举者。。
尝试
Board::PlayerPiece::PATROL
Just
PlayerPiece::PATROL
因为枚举是在类范围内使用的。
,如果您要接受公共成员功能的参数,则声明公众等枚举。
似乎有一个错字
#ifndef BOARD_H
#define BOARD_H
#ifndef BOARD_H
^^^^^^^^^^^^^^^
删除最后的#ifndef
相关文章:
- C ++枚举范围无法使用-std=c ++ 98进行编译,但使用-std=c ++ 11可以
- 如何在枚举上设置范围?
- 枚举范围无法通过 C++ 中的类中的结构访问
- 为什么可以访问范围之外的枚举器?
- 范围的枚举(枚举类)关系运营商
- 为什么可以使用范围运算符和类 A 名称在类 B 中访问类 A(公共)中的枚举
- 在C++14中,在哪个作用域中声明了重新声明枚举的非范围枚举器
- 在范围内添加位操作和转换式的枚举 - 圣诞节探索
- 枚举类别范围-C
- 是否可以知道非固定枚举者的范围
- C 在开关语句中使用特定类和枚举范围
- C++ 向前声明类范围的枚举
- C :枚举类型作为模板参数 - 全局范围
- 阵列索引(转换为整数)用范围枚举
- 使结构内定义的枚举具有全局范围C++
- 定义基于模板的映射指针,以便在验证范围时将 int 解析为枚举
- 如何使用无范围枚举器,就好像它的类型是它的基础类型一样
- 对象、枚举类型和范围的问题
- Project Euler#3获取一个Intellisense表达式,该表达式必须具有整型或无范围枚举类型
- C++错误:在使用运算符[]将元素插入std::map时,“表达式必须具有整型或非范围枚举类型”