尝试重载时'ostream'和'istream'错误C++
Error with 'ostream' and 'istream' in C++ trying to overloading
用templates
修改TAD
以使其成为通用(添加模板(:
对于输入/输出数据,我有此代码:
std::ostream& operator<<(std::ostream& os, Matrix & m)
{
os << m.rows() << " " << m.columns() << std::endl;
os << std::setprecision(4) << std::fixed;
for(int i=1; i <= m.rows(); i++)
{
for(int j=1; j <= m.columns(); j++)
{
os << m.value(i,j) << " ";
}
os << std::endl;
}
return os;
}
std::istream& operator>>(std::istream& is, Matrix& m)
{
int rows, columns;
float v;
is >> rows >> columns;
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=columns; j++)
{
is >> v;
m.assign(i,j,v);
}
}
return is;
}
定义(摘要(:
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include <stdexcept>
#include <iostream>
#include <iomanip>
template<typename E, int R, int C>
class Matriz
{
public:
Matrix();
private:
E elements_[R][C];
};
#include "matrix.cpp"
#include "matrix_io.cpp"
#endif // MATRIX_HPP
实现(摘要(:
#include <limits>
#include <cmath>
template<typename E, int R, int C>
Matrix<E,R,C>::Matrix()
{
for(int i=0; i<R; i++)
{
for(int j=0; j<C; j++)
{
elements_[i][j] = 0;
}
}
}
这是错误:
#include "matrix.hpp"
#define Element float
#define MatrixP Matrix<Element, 3, 3>
void tryBuildMatrix()
{
MatrixP m;
std::cout << m;
}
此错误:
错误:不能绑定'std :: ostream {aka std :: basic_ostream}' lvalue to'std :: basic_ostream&amp;&amp;'std :: cout&lt;&lt;m;
知道为什么会发生?
ps:如果我删除template
,则代码是完美的。
template<typename E, int R, int C>
std::ostream& operator<<(std::ostream& os, Matrix<E, R, C> & m)
{
...
}
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- 为什么我在使用 istream 迭代器时会出现 seg 错误?
- 关于 istream 中的错误处理的问题
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 尝试重载时'ostream'和'istream'错误C++
- 错误:无法将"std::istream {aka std::basic_istream<char>}"lvalue 绑定到"std::basic_istream&<char>&
- C - iStream错误标志
- 错误:"运算符>>"不匹配 重载 istream 运算符
- C++,错误:" no operator "== " matches these operands ",ostream/istream
- C++的 istream::eof() 的不一致是规范中的错误还是实现中的错误?
- 错误在模板类中调用过载的ISTream运算符
- C++ istream >> 和 ostream << 错误
- 错误:"与'运算符<<'不匹配(操作数类型为 std::istream)
- 错误 C2061:语法错误:标识符"istream"
- C++:在达到'end of file'或错误后重置 istream
- 在使用异常时改进istream助手类的错误处理
- 需要有关此编译错误的帮助 getline 函数错误:没有匹配函数调用"getline(std::istream&,char&)"|
- 尝试从istream存储令牌时出现分段错误
- 使失败并显示错误"cannot convert ‘std::istream {aka std::basic_istream<char>}’ to ‘bool’ in return"