访问在类外部声明的作用域枚举

Accessing scoped enums declared outside classes

本文关键字:作用域 枚举 声明 外部 访问      更新时间:2023-10-16

这是我当前的设置:

// test.h
enum class test_t {ONE, TWO, THREE, FOUR};
// test.cpp
#include "test.h"
// main.cpp
#include "test.h"
test_t thing = test_t::ONE;

但是,当我尝试在main.cpp中创建枚举对象时,我得到了error: expected a class or namespace。我在头中声明了作用域枚举,因为test.cppmain.cpp最终都需要访问它。

我目前的设置有问题吗?我对C++很陌生,所以我可能忽略了一些非常简单的东西。

这种类型的enum声明需要C++11进行编译。

在编译时,我需要将-std=c++11标志添加到g++中,以确保有支持。