繁体   English   中英

编译器错误:函数调用的常量表达式中必须具有常量值

[英]Compiler Error: function call must have a constant value in a constant expression

VS给我这样的错误:

 int seam[Image_height(img)];

我真的不明白为什么吗? 每次运行循环时,我都需要一个新的数组,并且数组的大小由传入的图像的高度定义。我还包括了它正在调用的函数。

int Image_height(const Image* img) {
    return img->height;
}

void horizontal_carve(Image *img, int newWidth) {

    ...

    int offset = Image_width(img) - newWidth;
    for (int i = 0; i < offset; ++i) {
        int seam[Image_height(img)];

        //do stuff with seam...

    }
}

有人可以解释为什么我得到错误

function call must have a constant value in a constant expression 

(特别是突出显示“ Image_height(img)”)以及如何解决? 谢谢!

C ++不支持可变长度数组,可以使用唯一的ptr

//int seam[Image_height(img)];
std::unique_ptr<int[]> seam(new int[Image_height(img)]); // c++11
auto seam = std::make_unique<int[]>(Image_height(img));  // c++14

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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