简体   繁体   中英

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

VS is giving me an error with the line:

 int seam[Image_height(img)];

and I don't really understand why? I need a fresh array each time I run the loop and it's size is defined by the height of the image I am passing in. I included the function that it is calling as well.

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...

    }
}

Could someone explain why I get the error

function call must have a constant value in a constant expression 

(in particular, highlighting "Image_height(img)") and what I could do to fix it? Thanks!

C++ does not support variable length arrays, you can use unique 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

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