【发布时间】:2019-03-05 03:18:09
【问题描述】:
我有一个二维的std::array,
std::array<std::array<string, n_height>, n_width> data_able;
n_height 和n_width 是常量变量,我不知道它们对于不同dataTables 的值,唯一可能的方法是使用函数调用:
const size_t n_height = dcmI_image->get_height();
const size_t n_width = dcm_image->get_width();
但这是不可能的,这是我得到的错误:
error: the value of ‘n_height’ is not usable in a constant expression
‘n_height’ was not initialized with a constant expression
当然,nWidth 也是如此。
【问题讨论】:
-
它们不仅需要保持不变,而且它们的值必须在编译时已知。即函数
getHeight和getWidth必须是constexpr并在编译时进行评估。 -
“常量表达式”(和相应的关键字
constexpr)决定编译时表达式 -
@VTT 我将我的 const 变量的声明更改为
constexpr size_t nHieght = dcmImage->getHeight();但我收到此错误:call to non-constexpr function ‘long unsigned int DicomImage::getHeight() const’所以,我猜函数定义必须是一个常量表达式。 -
std::vector是运行时长度数组的首选容器。如果在编译时不知道您的宽度/高度,请使用它。 -
@0x499602D2 谢谢老兄,我只是想试试,我试试看。