[英]Compilation error when typedef an std::array
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. 谢谢你的帮助。
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 typede
fing. 更改您的设计以使用
std::vector
或其他容器来克服问题或将常量放在标题中并在typede
之前包含它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.