[英]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.