2010-11-04 106 views
0

我試圖重載ostream的< <運營商在我的矩陣類,但我不斷收到以下錯誤:C++重載運算符<<在Matrix類

預計構造函數,析構函數或類型之前令牌&

轉換
Matrix::ostream& operator<<(const Matrix& matrix) 
{ 
    for (int r = 0; r < matrix.getNumrows(); r++) 
    { 
    cout << matrix.getPoint(r, 0); 
    for (int c = 0; c < matrix.getNumcolumns(); c++) 
    { 
     cout << " " << matrix.getPoint(r,c); 
    } 
    cout << endl; 
    } 

    return stream; 
} 

這是我的課

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include "Matrix.h" 

using namespace std; 

Matrix::Matrix() 
{ 

} 

Matrix::Matrix(int rows, int cols) { 
    numRows=rows; 
    numCols=cols; 

    //col=new double[cols]; 
    mx=new double*[rows]; 
    for (int i=0; i < rows; i++) { 
     mx[i] = new double[cols]; 
     // initalize each element of the new row. 
     for (int c=0; c < cols; c++) { 
      mx[i][c] = 0.0; 
     } 
    } 
} 


Matrix::Matrix(const Matrix &theMatrix) { 
    int rows=theMatrix.numRows; 
    int cols=theMatrix.numCols; 

    numRows = rows; 
    numCols = cols; 

    mx=new double*[rows]; 
    for (int r=0; r < rows; r++) { 
     mx[r] = new double[cols]; 
     // copy each element of the new row. 
     for (int c=0; c < cols; c++) { 
      mx[r][c] = theMatrix.mx[r][c]; 

     } 
    } 
} 


void Matrix::setMatrix(string file) 
{ 
    /* read the file */ 
    fstream inputStream(file.c_str()); 

    if(inputStream.is_open()) 
    { 
     string line; 
     stringstream ss; 

     getline(inputStream, line); 
     ss.clear(); 
     ss.str(line); 

     ss >> numRows >> numCols; 

     mx=new double*[numRows]; 
     for (int i=0; i < numRows; i++) { 
      mx[i] = new double[numCols]; 
      // initalize each element of the new row. 
      for (int c=0; c < numCols; c++) { 
       mx[i][c] = 0.0; 
      } 
     } 


     //now loop to get values 
     for(int row=0; row<numRows; row++) 
     { 
      getline(inputStream, line); 
      ss.clear(); 
      ss.str(line); 

      //now get every value in the line 
      for(int col=0; col<numCols; col++) 
      { 
       double current; 
       ss >> current; 
       mx[row][col] = current; 



      }//end reading values of row 

     }//end reading rows 

    } 

    //close the file 
    inputStream.close(); 
} 

int Matrix::getNumrows() 
{ 
    return numRows; 
} 

int Matrix::getNumcolumns() 
{ 
    return numCols; 
} 

void Matrix::printPoint() 
{ 
    for (int r=0; r < numRows; r++) 
    { 
     for (int c=0; c < numCols; c++) 
     { 
      cout << mx[r][c] << " "; 
     } 
     cout << endl; 
    } 
    cout << endl; 

} 

bool Matrix::getIsSquared() 
{ 
    if(numRows == numCols) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

double Matrix::det() 
{ 
    double det=0.0; 
    if(numRows!=numCols) 
    { 
     cout << "Number Rows must be same as number Colums\n"; 
    } 

    if(numRows==2) 
    { 
     det=(mx[0][0]*mx[1][1])-(mx[0][1]*mx[1][0]); 
    } 
    else 
    { 
     for(int i=0 ; i<numCols ; i++) 
     { 
      Matrix temp(numRows-1,numCols-1); 
      for(int j=0 ; j<numRows-1 ; j++) 
      { 
       for(int k=0 ; k<numCols-1 ; k++) 
       { 
        if(k<i) 
         temp.mx[j][k]=mx[j+1][k]; 
        else 
         temp.mx[j][k]=mx[j+1][k+1]; 
       } 
      } 
      det+=pow(-1.0,i)*mx[0][i]*temp.det(); 
     } 
    } 
    return det; 
} 

double Matrix::getPoint(int row, int col) 
{ 
    return mx[row][col]; 
} 

Matrix Matrix::operator +(const Matrix &right) const 
{ 
    Matrix result(numRows,numCols); 
    if (right.numRows != numRows || right.numCols != numCols) 
    { 
     cout << "\nError while adding matricies, the two must have the same dimentions.\n"; 
    } 
    else 
    { 
     for (int r=0; r < numRows; r++) 
     { 
      for (int c=0; c < numCols; c++) 
      { 
       result.mx[r][c] = (this->mx[r][c]) + (right.mx[r][c]); 
      } 
     } 
    } 

    return result; 
} 
+0

從輸入流中讀取時,需要確保處理可能發生的任何錯誤(例如,如果輸入數據與您期望的格式不匹配)。這是通過在每次輸入操作之後測試流的狀態來完成的。你的C++書應該詳細說明這是如何工作的。考慮使用某種形式的智能指針或像std :: vector這樣的標準庫容器來存儲矩陣類使用的存儲空間。正如所寫,您的班級不是例外安全的,很可能您的手動內存管理存在錯誤。 – 2010-11-04 02:48:41

回答

3

如果你想重載ostream的operator<<爲類,你需要因爲ostream對象上表達的左側(它寫成os << my_matrix)似乎使用一個友元函數還是非成員函數。

std::ostream& operator<<(std::ostream& os, const Matrix& matrix) { /* ... */ } 

看起來像你想實現它作爲一個成員函數,但實際上應該是這樣的:

std::ostream& Matrix::operator<<(const Matrix& matrix) { /* ... */ } 

,因爲當你實現一個操作符重載,因爲這將無法正常工作一個成員函數,表達式左邊的對象類型與重載是成員的類的類型相同(所以,在這種情況下,你必須編寫my_matrix1 << my_matrix2,它不是你想要什麼)。

在超載的內部,您不應該直接寫入cout;您應該寫入作爲參數傳遞給函數的ostream對象。

0

其餘只要改變

Matrix::ostream& operator<<(const Matrix& matrix) 

std::ostream& operator<< (std::ostream &stream, const Matrix &matrix) 

這將是一個獨立的功能..和應該只是罰款。

2

把它寫成:

ostream& operator<<(ostream& os, const Matrix& matrix) 
{ 
    for (int r = 0; r < matrix.getNumrows(); r++) 
    { 
     os << matrix.getPoint(r, 0); 
     for (int c = 0; c < matrix.getNumcolumns(); c++) 
     { 
      os << " " << matrix.getPoint(r,c); 
     } 
     os << endl; 
    } 
    return os; 
} 

,它會工作。它不一定是Matrix的成員函數。

+0

修正了這個問題,但現在我得到了: – Tony 2010-11-04 02:44:50

+0

錯誤:將'const Matrix'作爲'int'參數'int Matrix :: getNumrows()'丟棄限定符| – Tony 2010-11-04 02:46:11

+1

@Tony:如果你想在一個const限定的對象上調用成員函數,你需要對成員函數進行const限定。 – 2010-11-04 02:49:35