CPP | std流打印错误
CPP | std stream printing error
我目前正在编程一个int矩阵在cpp。
我想添加一个打印函数,以如下方式打印数字:
1 2 3
7 6 9
19 23 9
(每两个整数用空格隔开,行尾无空格)。所以我写了下面的代码:
std::ostream& IntMatrix::operator << (std::ostream& out) const
{
for(int i = 0; i < this->num_row; i++)
{
int j;
for(j = 0; j < this->num_col - 1; j++)
{
out << this->mat[i][j] << " ";
}
out << this->mat[i][j] << endl;
}
return out;
}
在文件IntMatrix.cpp中。但是,每次我尝试编译代码时,都会发生以下情况:
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
该怎么办?
不使用类成员函数,而是使用全局命名空间的重载:
std::ostream& operator << (std::ostream& out, const IntMatrix& m) {
}
并声明为IntMatrix
中的friend
相关文章:
- 为什么没有打印错误消息
- 插入排序打印错误的输出
- 当我输入一个字母时,cin 输入(输入是一个 int),而不是打印错误一次,它打印正确一次,然后在循环的其余部分打印 i
- 使用使用运算符<<并打印错误值 c++ 的类进行编程
- cLion & cMake 不调试/打印错误
- 如何使用适用于 Lua 的 C API 在控制台中打印错误
- 为什么我的代码打印错误的密文
- 声明数组和打印[错误?]
- 为什么在这种情况下打印错误
- q调试打印错误的数字
- 阿姆斯特朗数字打印错误
- C++ 链表打印错误
- 长双精度在 MinGW 上打印错误
- C++ <ctime> 打印错误的日期
- 用"fprintf"打印错误后调用"getchar()"有什么意义?
- C++类型包装器 printf 打印错误的浮点值
- 如何让犰狳函数在反转奇异矩阵时不打印错误
- 缩短的数组打印错误的结果
- 打印错误的链表c++
- cout 中的打印错误