使用模板友元函数c ++无效使用非静态数据成员错误

时间:2014-09-14 20:26:13

标签: c++ templates file-io multidimensional-array friend

#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文件。我试图重载&gt;&gt;运营商。在“朋友istream&amp;运算符&gt;&gt;(istream&amp; inStream,Matrix&amp; other)”函数我试图从文本文件加载数据,但我一直收到invalid use of non-static data member错误。

1 个答案:

答案 0 :(得分:1)

this函数中没有隐式friend,因此您无法使用非静态数据成员。您必须使用适当的名称访问Matrix的成员,例如other.matrix