0

我正在使用 Opencv/C++。我使用该函数获取视频中的帧数 int noOfFrames = cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_COUNT );

我还声明了一个数组int Entropy[noOfFrames];。但是由于该变量noOfFrames是非常量的,因此会出错。

我什至用过const_cast这个,但它仍然给出了一个错误。我希望数组的长度等于视频的帧数。

我该怎么做 ???

4

2 回答 2

6

您不能声明具有动态大小的静态数组。你需要一个动态数组:

int* Entropy = new Entropy[noOfFrames];

// use here, same as array

delete[] Entropy;

但是使用向量更容易:

std::vector<int> Entropy(noOfFrames);

// use here, same as array and more

// no need to clean up, std::vector<int> cleans itself up
于 2013-01-01T20:03:53.423 回答
5

在 C++ 中,您不能这样做,因为 c 样式数组的大小应该是编译时常量1

无论如何,你有一个更好的选择:使用std::vector

std::vector<int> Entropy(noOfFrames);

即使您有编译时常量,我也不建议您使用int arr[size]哪个是 c 样式的数组。相反,我建议您使用std::array<int,size> arr;这又是一个更优越的解决方案。

于 2013-01-01T20:03:40.523 回答