0

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!

WedaPashi
  • 3,561
  • 26
  • 42
Max
  • 1
  • 1
    Use a `std::vector`. – tkausl Jul 10 '18 at 00:49
  • Unfortunately I'm required to use an array for this project, thanks though – Max Jul 10 '18 at 00:52
  • 2
    Array size must be known at compile-time – M.M Jul 10 '18 at 00:57
  • 1
    Possible duplicate of [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – phuclv Jul 10 '18 at 04:17
  • @Max : Whatever your assignment dictates, just be aware that arrays are the wrong tool for the job here. – ildjarn Jul 10 '18 at 05:52
  • 3
    _"Unfortunately I'm required to use an array for this project"_ Are you really absolutely sure about that? Using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) is the correct answer, it is a variable length array, it is part of C++, available to you with `#include `, it is not an external library or extension. If you want to learn C++ don't avoid it. – acraig5075 Jul 10 '18 at 07:24

1 Answers1

0

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
Johnny
  • 9
  • 3