typedef std :: array时的编译错误

时间:2022-09-26 21:31:30

I define a type called inputTy using std::array (c++11) , the dimension of the array declared as an external constant integer d.

我使用std :: array(c ++ 11)定义了一个名为inputTy的类型,该数组的维度被声明为外部常量整数d。

namespace project {
  namespace types{
    extern const int d;
    typedef std::array<double, d> inputTy;
  }
}

Why do I get such compilation error?

为什么我会收到这样的编译错误?

../../include/types.h:16:29: error: the value of ‘d’ is not usable in a constant expression
 typedef std::array<double, d> inputTy;
                             ^
../../include/types.h:15:18: note: ‘d’ was not initialized with a constant expression
 extern const int d;
              ^

Thanks for your help.

谢谢你的帮助。

1 个解决方案

#1


You can not use extern const int as array size because compiler does not know the size of a constant from other compilation unit.

您不能将extern const int用作数组大小,因为编译器不知道来自其他编译单元的常量的大小。

Change your design to use std::vector or some other container to overcome the problem or put the constant in a header and include it before typedefing.

更改您的设计以使用std :: vector或其他容器来克服问题或将常量放在标题中并在typedefing之前包含它。

#1


You can not use extern const int as array size because compiler does not know the size of a constant from other compilation unit.

您不能将extern const int用作数组大小,因为编译器不知道来自其他编译单元的常量的大小。

Change your design to use std::vector or some other container to overcome the problem or put the constant in a header and include it before typedefing.

更改您的设计以使用std :: vector或其他容器来克服问题或将常量放在标题中并在typedefing之前包含它。