4

我正在尝试编译在以下行中显示错误的应用程序:

enum class HoleMaskPixelTypeEnum {HOLE, VALID, UNDETERMINED};

我没有使用 enum 和 class 关键字这样的方式。如果我评论关键字类,则会发生以下错误

error C2864: 'HolePixelValueWrapper<T>::Value' : only static const integral data members can be initialized within a class

在以下代码中:

 template <typename T>
struct HolePixelValueWrapper
{
  HolePixelValueWrapper(const T value) : Value(value){}

  operator T()
  {
    return this->Value;
  }

  T Value = 0;//error here.
};

没办法解决。

4

3 回答 3

8

Scoped enumerations (enum class) and in-class initialisation of member variables are a fairly new language features (introduced in C++11); according to this table, the former needs Visual Studio 11.0 or later, and the latter is not yet supported.

If your compiler doesn't support scoped enumerations, then the only option is to remove class. You might consider scoping it inside a class or namespace, if you don't want to cause wider pollution.

如果它不支持类内初始化,那么您只需要在构造函数中以老式方式进行即可。但是,无论如何在这里使用它是没有意义的,因为该成员是由唯一的构造函数初始化的。只需删除= 0.

于 2013-11-07T09:31:19.843 回答
3

enum class Blah是 C++11 的一个特性。您是否使用 C++11 编译器进行编译?

于 2013-11-07T09:25:14.700 回答
0

关于在enum class定义中使用 C++11 的问题,在描述HolePixelValueWrapper您尝试Value在声明它的同一位置初始化类成员的代码中,这是不可能的(不是在 C++11 中),只有静态类成员被允许像那样被初始化。删除= 0就OK了。

If you want to keep initializing Value to zero, you can just make your constructor's parameter to have value by default, like that (of course if it's not breaking your design):

HolePixelValueWrapper(const T value = 0) : Value(value){}

于 2013-11-07T09:31:10.707 回答