简体   繁体   中英

Overloading << operator to output matrix in C++

Hi im trying to overload the << operator in order to output a matrix as a table. I have started implementing and have finished it, but im not sure if i did it right as im getting errors that dont make sense to me. This is my 1st time overloading << so im not sure if i even did this right. Please help.

Error(edited):

In file included from MatrixTester.cpp:3:
Matrix.h:16: error: expected ‘;’ before ‘operator’
In file included from MatrixTester.cpp:4:
Matrix.cpp:132: error: ‘std::ostream& Matrix::operator<<(std::ostream&, const Matrix&)’ must take exactly one argument
MatrixTester.cpp: In function ‘int main()’:
MatrixTester.cpp:15: error: no match for ‘operator<<’ in ‘std::cout << matrix’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (.....

My MatrixTester.cpp:

#include "Matrix.h"
#include "Matrix.cpp"
#include<iostream>
#include<vector>
using namespace std;

int main(){
    int row,column;
    cout << "Enter number of rows: ";
    cin >> row;
    cout << "Enter number of column: ";
    cin >> column;
    Matrix matrix(row,column);
    cout<<matrix;
    vector<int> rowVec;
    vector<int> colVec;
    bool cont = true;

    do{
        cout << "Enter column index: ";
        int index;
        cin >> index;

        for(int i = 0; i < row; i++){
            cout << "Enter an integer: ";
            int num;
            cin >> num;
            colVec.push_back(num);  
        }
        if (matrix.setColumn(colVec,index) == false){
            cout << "Unable to modify matrix: Index might be out of bounds " << endl;
        }else{
            cont = false;
        }
    }while(cont == true);
    cout<<matrix;   

    cout << "Enter row index: ";
    int index;
    cin >> index;
    rowVec = matrix.getRow(index);
    for (int i =0;i<rowVec.size();i++){
        cout << rowVec[i] << "  ";
    }
    cout << endl;

    double avg = matrix.average();
    cout<<"Average: " <<avg<<endl;

    Matrix m2(rowVec.size(), colVec.size());
    rowVec = matrix.getRow(1);
    for (int i=0; i <= row-1; i++){
        cout <<"for l" <<endl;
        m2.setRow(rowVec,i);
    }

    cout <<"Adding..." <<endl;
    Matrix m3(row,column); 
    m3 = matrix + m2;
    cout<<m3;

    cout <<"Subtract..." <<endl; 
    m3 = matrix - m2;
    cout<<m3;

    cout <<"Multiply..." <<endl; 
    m3 = matrix * m2;
    cout<<m3;
}

Matrix.h

#ifndef MATRIX_H
#define MATRIX_H 

#include<vector>
using namespace std;

class Matrix{
    public:
        Matrix();
        Matrix(int r, int c);
        bool setRow(vector<int> row, int r);
        bool setColumn(vector<int> col, int c);
        vector<int> getRow(int index);
        vector<int> getCol(int index);
        void indentityMatrix();
        ostream& Matrix operator<<(ostream& out, const Matrix& m);
        double average();
        Matrix operator+(Matrix m);
        Matrix operator-(Matrix m);
        Matrix operator*(Matrix m);
        Matrix operator/(Matrix m);
    private:
        int row, column;
        vector< vector<int> > matrix;
};

#endif

Overload operator<< in Matrix.cpp (edited)

ostream& Matrix::operator<<(ostream& out, const Matrix& m){
    vector<int> vecRow;
    for (int i = 0; i < row; i++){
        vecRow = m.getRow(i);
        for (int j = 0; j < row; j++){
            out << vecRow[j];
            out <<"\t";
        }
        out << endl;
    }
    return out;
} 

Im still getting the errors

The reason for the error is that you have #include "Matrix.cpp" in your code. It is wrong all by itself, but in this case it also causes the "incomplete type" error, because it is included ahead of the <iostream> header.

You should never need to #include a cpp file. If you did so to fix the link issue because operator << has been missing, the proper way to fix it is to compile your cpp files on the same command, compile them separately without linking and then linking, or to include them as separate items of your IDE project (if you are using a development environment).

Note that you are passing Matrix to the operator by value. This is suboptimal, because your entire matrix gets copied simply for the purpose of being printed. Generally, you should pass the item being printed to the operator << by constant reference:

// This goes into the Matrix.h header:
friend std::ostream & operator<<(std::ostream &out, const Matrix& p);

// This goes into Matrix.cpp implementation:
ostream& operator<<(ostream& out, const Matrix& m) {
    ...
}

EDIT (Thanks for a great hint, Mooing Duck!)

Since dasblinkenlight seems to be temporarily distracted, here's how the operator<< should be declared and defined:

Matrix.h:

class Matrix{
    public:

        //ostream& Matrix operator<<(ostream& out, const Matrix& m);
        friend ostream& operator<<(ostream& out, const Matrix& m);

};

Matrix.cpp:

ostream& operator<<(ostream& out, const Matrix& m) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM