最初,矢量“perm”为空。我想根据循环的索引将其设置为零。但我在执行以下代码时遇到错误“向量下标超出范围”。
我在任何循环之外的“Start()”方法中放置了“perm(charLength,0)”,以便它不会被重置,它用于累积值。
DecodeEngine.h
class DecodeEngine
{
public:
vector<int> temp;
vector<int> perm;
//Default constructor
DecodeEngine();
//Declare a virtual destructor:
virtual ~DecodeEngine();
//Methods
string GetFilePath();
Mat Start();
void FrameTo8by8();
};
DecodeEngine.cpp
Mat DecodeEngine::Start()
{
charLength = 160;
//Initialize perm to zero
perm(charLength, 0);
//Loop 1st 100 frame of header
while(true)
{
if(frame_count <= 100)
{
FrameTo8by8(); //Proccess and algorithm
namedWindow("dctBlockImage");
imshow("dctBlockImage", dctImage); //display watermarked image
if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
frame_count++;
}
else
{
cout << endl;
cout << "End of video" << endl;
cout << endl;
destroyWindow("Original Video");
destroyWindow("dctBlockImage");
break;
}
}
void DecodeEngine::FrameTo8by8()
{
for(int i = 0; i < height-16; i += 16)
{
for(int j = 0 ; j < width-16; j += 16)
{
if(j > 112)
{
if(((sum4 / 4) - avg) > 0)
{
value = 0;
temp.push_back(value);
}
else
{
value = 1;
temp.push_back(value);
}
}
if(temp.size() == charLength)
{
for(int a = 0; a <= temp.size(); a ++)
{
//Initialize perm to zero
perm[a] = 0;
if(temp[a] == 1)
{
perm[a]++;
}
//Reset temp for next frame to use
temp[a] = 0;
}
}
}
}
}
答案 0 :(得分:2)
这一行:
perm(charLength, 0);
不调用构造函数std::vector(size_t, const T&)
并使用值为10
的160个元素初始化向量。事实上,由于vector类没有operator()
,我根本看不到编译的方式。
std::vector
包含一个成员函数assign()
,您可以改为:
perm.assign(charLength, 0);
答案 1 :(得分:0)
在下面的代码中,您超出了范围:
...
if(temp.size() == charLength) // ok! temp.size() is same as perm.size()
{
for(int a = 0; a <= temp.size(); a ++) // !!! loop will execute with a=temp.size() and stop afterwards
{
//Initialize perm to zero
perm[a] = 0; // !!! but subscripts start with 0 and go to temp.size()-1