"no matching function for call" 当试图 cin.read 进入二维数组时
"no matching function for call" when trying to cin.read into a 2d array
Pixel是一个包含3个字符的结构。
struct Pixel { char r, g, b} ;
int H = 5, C = 10;
Pixel *PMatrix[H]; // Creates an array of pointers to pixels
for (int h = 0 ; r < H ; h++) {
PMatrix[r] = new Pixel[C]; //Each row points to an array of pixels
}
我有一个 PPM 文件,我正在尝试逐行将字节读入我的像素矩阵以表示图像。
for (unsigned int i = 0; i < height; i++){
cin.read(PMatrix[i][0], width*3);
}
我也在循环中尝试了 "cin.read(PMatrix[i], width*3);"
。
我收到错误 no matching function for call to 'std::basic_istream<char>::read(PpmImage::Pixel&, unsigned int)'
这是什么意思???
错误是您创建了 class 并将其传递给没有重载的标准库函数。 PMatrix
是一个 Pixel*[]
,所以使用 []
一次得到一个 Pixel*
,然后再次给出一个 Pixel
。 cin.read
对 Pixel
一无所知,也没有运算符来处理它。
通常,人们会因为 class 和 istream
.
而使 operator>>
过载
std::istream& operator>>(std::istream& lhs, Pixel& rhs)
{
lhs >> rhs.r >> rhs.g >> rhs.b;
return lhs;
}
//...
cin >> PMatrix[i][0]; //calls our overloaded operator
我不确定,但我认为您可能一直在尝试这样做:
cin.read(reinterpret_cast<char*>(PMatrix[i]), 3); //ew magic number
由于 Pixel
是 POD 类型,您可以将其转换为指向第一个元素的指针。这将读取三个 char
并将它们存储到 PMatrix[i][0]
中。不过,我建议使用第一种方法。它更惯用,看起来更稳定。
Pixel是一个包含3个字符的结构。
struct Pixel { char r, g, b} ;
int H = 5, C = 10;
Pixel *PMatrix[H]; // Creates an array of pointers to pixels
for (int h = 0 ; r < H ; h++) {
PMatrix[r] = new Pixel[C]; //Each row points to an array of pixels
}
我有一个 PPM 文件,我正在尝试逐行将字节读入我的像素矩阵以表示图像。
for (unsigned int i = 0; i < height; i++){
cin.read(PMatrix[i][0], width*3);
}
我也在循环中尝试了 "cin.read(PMatrix[i], width*3);"
。
我收到错误 no matching function for call to 'std::basic_istream<char>::read(PpmImage::Pixel&, unsigned int)'
这是什么意思???
错误是您创建了 class 并将其传递给没有重载的标准库函数。 PMatrix
是一个 Pixel*[]
,所以使用 []
一次得到一个 Pixel*
,然后再次给出一个 Pixel
。 cin.read
对 Pixel
一无所知,也没有运算符来处理它。
通常,人们会因为 class 和 istream
.
operator>>
过载
std::istream& operator>>(std::istream& lhs, Pixel& rhs)
{
lhs >> rhs.r >> rhs.g >> rhs.b;
return lhs;
}
//...
cin >> PMatrix[i][0]; //calls our overloaded operator
我不确定,但我认为您可能一直在尝试这样做:
cin.read(reinterpret_cast<char*>(PMatrix[i]), 3); //ew magic number
由于 Pixel
是 POD 类型,您可以将其转换为指向第一个元素的指针。这将读取三个 char
并将它们存储到 PMatrix[i][0]
中。不过,我建议使用第一种方法。它更惯用,看起来更稳定。