尝试读取 2D 数组时"no matching function for call"
"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)'
这是什么意思???
错误在于您创建了一个类,并且您正在将其传递给没有重载的标准库函数。 PMatrix
是一个Pixel*[]
,所以使用[]
一次得到一个Pixel*
,然后再次给出一个Pixel
。 cin.read
对Pixel
一无所知,也没有操作员来处理它。
通常,一个人会为他们的班级和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]
中。不过,我建议使用第一种方法。它更惯用,看起来不那么不稳定。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- 为什么有一个指向此代码行(第 7 行)的"no matching call for function"错误?