Home > Enterprise >  Addition of Matrix in C Object Oriented Programming
Addition of Matrix in C Object Oriented Programming

Time:03-28

The following code gives me an error on the friend function: [Error] no 'Matrix Matrix::Sum(Matrix, Matrix)' member function declared in class 'Matrix'. Maybe there is any mistake in using of friend function in main the function. Kindly suggest me: The following code gives me an error on the friend function: [Error] no 'Matrix Matrix::Sum(Matrix, Matrix)' member function declared in class 'Matrix'. Maybe there is any mistake in using of friend function in main the function. Kindly suggest me:

#include <iostream>
#include <string.h>
using namespace std;

class Matrix{
    private:
        int noOfRows;
        int noOfColumns;
        int **data;
    public:
        Matrix(int noOfRows, int noOfColumns);
        void displayData();
        ~Matrix();
        Matrix (const Matrix &ref);//copy constructor
        Matrix operator   (Matrix m);//Operator overloading
        friend Matrix Sum(Matrix,Matrix);//for sum of two matrix
};

Matrix::Matrix(int inr=0, int inc=0){
        noOfRows=inr; noOfColumns=inc;
        data=new int*[noOfColumns];
        for(int i=0;i<noOfRows;i  )
            data[i]=new int[noOfColumns];
        int d;
        for(int r=0;r<noOfRows;r  ){
            for(int c=0;c<noOfColumns;c  ){
                cout<<"Enter ...";cin>>d;
                data[r][c]=d;
            }
            cout<<endl;
        }
    }

Matrix::Matrix (const Matrix &ref){
    this->data=new int*[ref.noOfColumns];
    for(int i=0;i<ref.noOfRows;i  )
        this->data[i]=new int[ref.noOfColumns];
    
    for(int r=0;r<ref.noOfRows;r  ){
        for(int c=0;c<ref.noOfColumns;c  ){
            this->data[r][c]=ref.data[r][c];
            cout<<this->data[r][c]<<"\t";
        }
        cout<<endl;
    }
}

Matrix Matrix::operator   (Matrix m){
    Matrix ms;
    ms.noOfRows=noOfRows m.noOfRows;
    ms.noOfColumns=noOfColumns m.noOfColumns;
    return ms;
   }

Matrix Matrix::Sum(Matrix m1,Matrix m2){
    Matrix m;
    m=m1 m2;
    return m;       
}

void Matrix::displayData(){
    for(int r=0;r<noOfRows;r  ){
        for(int c=0;c<noOfColumns;c  )
            cout<<data[r][c]<<"\t";
        cout<<endl;
    }
}

Matrix::~Matrix(){
    delete[] data;
}

int main(){
    Matrix M1(2,2),M2(2,2),M3;
    cout<<"\n Matrix A="<<endl;
    M1.displayData();
    cout<<"\n Matrix B="<<endl;
    M2.displayData();
    M3=Sum(M1,M2);
    M3.displayData();
    return 0;
}

CodePudding user response:

The problem is that you have declared Sum as a friend function of class Matrix while defined it as a member function.

To solve the mentioned error just remove the Matrix:: qualification while defining it as shown below:

//----v-------------------------->removed `Matrix::` from here
Matrix Sum(Matrix m1,Matrix m2){
    Matrix m;
    m=m1 m2;
    return m;       
}

Also, the program may have other logical errors. You can refer to the rule of three for more information about them or ask a separate question if that doesn't help.

CodePudding user response:

I'm not a matrix specialist, but I understood that matrix summation required both matrixes to be of the same size, and each elements to be summed up.

So you need to fully redefine operator (to avoid introducing exceptions here, I'd taken a permissive mathematical view, taking the max size of both matrixes and considering elements out of bounds to be 0):

Matrix Matrix::operator   (Matrix m){
    Matrix ms(max(noOfRows,m.noOfRows), max(noOfColumns m.noOfColumns));
    for (int i=0; i<ms.noOfRows; i  ) 
        for (int j=0; j<ms.noOfColumns; j  ) 
           ms.data[i][j] = (i<noOfRows&&j<noOfColumns ? data[i][j]:0.0)
                               (i<m.noOfRows&&j<m.noOfColumns ? m.data[i][j]:0.0);
    return ms;
}

By the way, it'll be safer to use the signature Matrix operator (const Matrix& m) const, to avoid that a typo could accidentally change the value of the matrix, and avoid an unnecessary copy of the matrix argument.

Then, you must make Sum() a free standing function instead of a member function, if you want to call it like you do in main().

  • Related