【发布时间】:2018-07-10 00:46:15
【问题描述】:
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)”)以及我能做些什么来解决它? 谢谢!
【问题讨论】:
-
使用
std::vector。 -
不幸的是,我需要为这个项目使用一个数组,不过谢谢
-
数组大小必须在编译时知道
-
“很遗憾,我需要为这个项目使用一个数组” 你真的完全确定吗?使用
std::vector是正确的答案,它是一个可变长度数组,它是C++ 的一部分,你可以使用#include <vector>,它不是一个外部库或扩展。如果您想学习 C++,请不要避免。
标签: c++ c++11 visual-c++