#include <cassert>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
template <class T> class Matrix;
template <class T>
class Matrix
{
public:
friend istream& operator>>(istream& inStream,Matrix& other)
{
string line ;
T x ;
unsigned _rows = 0 , _cols = 0 ;
while(inStream.good())
{
while(getline(inStream,line))
{
istringstream streamA(line) ;
_cols = 0;
while(streamA >> x)
{
matrix[_rows][_cols] = x ;
_cols++ ;
}
_rows++ ;
}
}
return inStream ;
}
Matrix(unsigned r, unsigned c);
Matrix(const Matrix<T>& rhs);
~Matrix();
const Matrix operator=(const Matrix& other);
void output() const ;
// Matrix mathematical operations: insert overloaded operator signatures
// Access the individual elements of a matrix: insert overloaded operator signatures
// Getters:
unsigned getRows() const; // Return number of rows
unsigned getCols() const; // Return number of columns
private:
Matrix();
T ** matrix; // the matrix array
unsigned rows; // # rows
unsigned cols; // # columns
上面是我的.h文件。我正在尝试使 >> 运算符重载。在“friend istream& operator>>(istream& inStream,Matrix& other)”函数中,我试图从文本文件加载数据,但我一直收到 invalid use of non-static data member
错误。
最佳答案
friend
函数中没有隐式的this
,所以你不能只使用非静态数据成员。您必须使用适当的名称访问您的 Matrix
的成员,例如 other.matrix
。
关于c++ - 使用模板友元函数 C++ 无效使用非静态数据成员错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837683/