简体   繁体   English

typedef std :: array时的编译错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM