简体   繁体   中英

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 .

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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