我觉得这是一个愚蠢的问题,但我很累,所以我只想问这里。我的矢量大小从2变为0.首先我调用过程:
void PPM::process(pmode_t pmode, const string &fname)
{
// read qcolors in fname
read_qcolors(fname);
// run proper process mode
if (pmode == run_process1) process1();
else process2();
return;
}
调用read_qcolors(),它创建一个大小为2的向量:
void PPM::read_qcolors(const string &fname)
{
// open fname: check status
//
// while (more data0 {
// read R G B values
// save to qcolors vector
// }
//
// close input file
ifstream filestream;
vector <RGB> qcolors;
string line;
stringstream ss;
unsigned int ui1, ui2, ui3;
cerr << "reading " << fname << '\n';
filestream.open(fname.c_str());
if (filestream.fail()) cerr << "qcolors file failed to open.\n";
while (getline(filestream, line)) {
if (filestream.eof()) break;
if (filestream.fail()) cerr << "error during reading of qcolors file.\n";
RGB rgb;
ss.str(line);
/* Apparently there are no formatting flags to tell
a stringstream to read a stream as uchars. So I store
the stream into insigned ints, then convert the unsigned
ints to unsigned chars when assigning the RGB values. */
ss >> ui1 >> ui2 >> ui3;
rgb.R = (unsigned char)ui1;
rgb.G = (unsigned char)ui2;
rgb.B = (unsigned char)ui3;
//empty stringstream
ss.str("");
//reset state flags
ss.clear();
qcolors.push_back(rgb);
}
filestream.close();
cerr << "qcolor size is " << qcolors.size() << '\n';
return;
}
之后返回process()调用process1(),此时向量大小再次为0:
void PPM::process1()
{
// for each pixel {
// find closest qcolor
// set pixel color to closest qcolor
// }
cerr << "img size: " << img.size() << '\n';
cerr << "qcolor size: " << qcolors.size() << '\n';
float distance, min_distance;
int min_index;
//go thru each element in the vector of the img pixels
for (int i=0; i<img.size(); i++) {
for (int j=0; i<qcolors.size(); j++) {
distance = img[i].distance(qcolors[j]);
cerr << "calced distance of " << distance << '\n';
if (distance < min_distance || j == 1) {
min_distance = distance;
min_index = j;
cerr << "set min distance to " << min_distance << '\n';
}
}
img[i] = qcolors[min_index];
}
return;
}
答案 0 :(得分:4)
您似乎在隐藏您的成员变量(在read_qcolors中):
vector <RGB> qcolors;
在这种情况下只需删除此行,它将使用成员变量。