为不适用于输出流的模板矩阵重载“+”
Overloading "+" for template matrices not working with output stream
我正在尝试重载 + 以将两个矩阵相加,然后立即输出。例如:
matrix<int> a, b;
...
cout << a + b << endl; //doesn't work
matrix<int> c = a + b; //works
cout << a << endl; //works
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
c:\mingw\lib\gcc\mingw32.8.1\include\c++\ostream|602|error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = matrix<int>]'|
我已经超载了<<,但我不确定如何让它协同工作。到目前为止,这是我所拥有的:(<< 适用于单个矩阵)
template <typename Comparable>
class matrix
{
private:
size_t num_cols_;
size_t num_rows_;
Comparable **array_;
public:
friend ostream& operator<< (ostream& o, matrix<Comparable> & rhs){
size_t c = rhs.NumRows();
size_t d = rhs.NumCols();
for (int i = 0; i < c; i++){
for (int j = 0; j < d; j++){
o << rhs.array_[i][j] << " ";
}
o << endl;
}
return o;
}
matrix<Comparable> operator+ (matrix<Comparable> & rhs){
matrix<Comparable> temp(num_rows_,num_cols_);
for (int i = 0; i < num_rows_; i++){
for (int j = 0; j < num_cols_; j++){
temp.array_[i][j] = array_[i][j] + rhs.array_[i][j];
}
}
return temp;
}
}
a + b
表达式根据为 matrix<T>::operator+
声明的 return 类型产生纯右值:
matrix<Comparable> operator+ (matrix<Comparable> & rhs);
~~~~~~~~~~~~~~~~~^
反过来,operator<<
需要一个可修改的左值:
friend ostream& operator<< (ostream& o, matrix<Comparable> & rhs);
~~~~~~~~~~~~~~~~~~~^
由于 operator<<
不应修改其参数,您可以安全地将其转换为 const
左值引用(只要 NumRows()
和 NumCols()
是 const
个合格的成员函数):
friend ostream& operator<< (ostream& o, const matrix<Comparable> & rhs);
~~~~^
旁注:operator+
还应将其操作数作为 const
引用,并且其自身应 const
合格(如果它保留为成员函数):
matrix<Comparable> operator+ (const matrix<Comparable> & rhs) const;
~~~~^ ~~~~^
我正在尝试重载 + 以将两个矩阵相加,然后立即输出。例如:
matrix<int> a, b;
...
cout << a + b << endl; //doesn't work
matrix<int> c = a + b; //works
cout << a << endl; //works
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'|
c:\mingw\lib\gcc\mingw32.8.1\include\c++\ostream|602|error: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = matrix<int>]'|
我已经超载了<<,但我不确定如何让它协同工作。到目前为止,这是我所拥有的:(<< 适用于单个矩阵)
template <typename Comparable>
class matrix
{
private:
size_t num_cols_;
size_t num_rows_;
Comparable **array_;
public:
friend ostream& operator<< (ostream& o, matrix<Comparable> & rhs){
size_t c = rhs.NumRows();
size_t d = rhs.NumCols();
for (int i = 0; i < c; i++){
for (int j = 0; j < d; j++){
o << rhs.array_[i][j] << " ";
}
o << endl;
}
return o;
}
matrix<Comparable> operator+ (matrix<Comparable> & rhs){
matrix<Comparable> temp(num_rows_,num_cols_);
for (int i = 0; i < num_rows_; i++){
for (int j = 0; j < num_cols_; j++){
temp.array_[i][j] = array_[i][j] + rhs.array_[i][j];
}
}
return temp;
}
}
a + b
表达式根据为 matrix<T>::operator+
声明的 return 类型产生纯右值:
matrix<Comparable> operator+ (matrix<Comparable> & rhs);
~~~~~~~~~~~~~~~~~^
反过来,operator<<
需要一个可修改的左值:
friend ostream& operator<< (ostream& o, matrix<Comparable> & rhs);
~~~~~~~~~~~~~~~~~~~^
由于 operator<<
不应修改其参数,您可以安全地将其转换为 const
左值引用(只要 NumRows()
和 NumCols()
是 const
个合格的成员函数):
friend ostream& operator<< (ostream& o, const matrix<Comparable> & rhs);
~~~~^
旁注:operator+
还应将其操作数作为 const
引用,并且其自身应 const
合格(如果它保留为成员函数):
matrix<Comparable> operator+ (const matrix<Comparable> & rhs) const;
~~~~^ ~~~~^