簡體   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