2014-09-14 1 views
0
#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 파일입니다. >> 연산자를 오버로드하려고합니다. "친구 istream & 연산자 (istream & inStream, 매트릭스 & 기타)"function 텍스트 파일에서 데이터를로드하려고하지만 계속해서 invalid use of non-static data member 오류가 발생합니다.템플릿 친구 기능과 함께 비 정적 데이터 멤버 오류가 잘못되었습니다. C++

+0

'matrix [_rows] [_ cols] = x'' 대신에'other.matrix [_rows] [_ cols]'를 의미했을 수도 있습니다. –

답변

1

friend 함수에는 암시 적으로 this이 없으므로 비 정적 데이터 멤버 만 사용할 수 없습니다. Matrix의 회원에게 적절한 이름 (예 : other.matrix)을 사용하여 액세스해야합니다.

관련 문제