Perform Matrix Operations using templates and operator overloading in C++

1067 views c++
8

Here is my full code where I have performed addition,subtraction and transpose operations on matrices using templates and operator overloading. WHEN I DO IT WITH MEMBER FUNCTIONS , EVERYTHING IS FINE. BUT WHEN I TRY TO DO IT WITH FRIEND FUNCTION AS DONE BELOW, ERROR(mentioned later) IS SHOWN. Can somebody please help me to correct this code.

#include<iostream>
using namespace std;

template<class T>
class mat
{
    int r,c;
    T m[5][5];
    public:
    mat(){}
    int check(mat);
    void get();
    void show();
    friend mat<T> operator+(mat<T>, mat<T>);
    mat<T> operator-(mat<T>);
    mat<T> operator*(mat<T>);
    mat<T> operator!();
};

template<class T>
int mat<T>::check(mat<T> B)
{
    if(r==B.r && c==B.c)
        return 0;
    return -1;
}

template<class T>
void mat<T>::get()
{
    cout<<"Enter the no of rows and columns ";
    cin>>r>>c;
    for(int i=0;i<r;i++)
    for(int j=0;j<c;j++)
    {
        cout<<"Enter element a"<<i<<j<<" : ";
        cin>>m[i][j];
    }
}

template<class T>
void mat<T>::show()
{
    for(int i=0;i<r;i++)
    {
    for(int j=0;j<c;j++)
        cout<<m[i][j]<<"\t";
    cout<<endl;
    }
}

template<class T>
mat<T> operator+(mat<T> A,mat<T> B)
{
    mat<T> C;
    C.r=A.r; C.c=A.c;
    for(int i=0;i<A.r;i++)
    for(int j=0;j<A.c;j++)
    C.m[i][j]=A.m[i][j]+B.m[i][j];
    return C;
}

template<class T>
mat<T> mat<T>::operator-(mat<T> B)
{
    mat C;
    C.r=r; C.c=c;
    for(int i=0;i<r;i++)
    for(int j=0;j<c;j++)
    C.m[i][j]=m[i][j]-B.m[i][j];
    return C;
}

template<class T>
mat<T> mat<T>::operator*(mat<T> B)
{
    mat C;
    C.r=r; C.c=B.c;

    for(int i=0;i<r;i++)
    {
        for(int j=0;j<B.c;j++)
        {
            C.m[i][j]=0;
            for(int k=0;k<c;k++)
            {
                C.m[i][j]+=(m[i][k]*B.m[k][j]);
            }
        }
    }
    return C;
}

template<class T>
mat<T> mat<T>::operator!()
{
    mat C;
    C.r=c; C.c=r;;
    for(int i=0;i<r;i++)
    for(int j=0;j<c;j++)
    C.m[j][i]=m[i][j];
    return C;
}

int main()
{
    int ch,r1,r2,c1,c2,ch1;

      mat<int> m1,m2,m3;
      mat<float> m4,m5,m6;
    do
    {
        cout<<"\n***MENU***\n1-Enter matrices\n2-Show matrices\n3-Add\n4-Subtract\n5-Multiply\n6-Transpose\n7-Exit\nPlease selct a choice ";
        cin>>ch;
        switch(ch)
        {
            case 1:
                    cout<<"For integer matrices\n";
                    cout<<"For the first matrix \n";
                    m1.get();
                    cout<<"For the second matrix \n";
                    m2.get();
                    cout<<"\nFor float matrices\n";
                    cout<<"For the first matrix \n";
                    m4.get();
                    cout<<"For the second matrix \n";
                    m5.get();
                break;

            case 2:
                cout<<"For integer matrices\n";
                cout<<"First matrix \n";
                m1.show();
                cout<<"Second matrix \n";
                m2.show();
                cout<<"\nFor float matrices\n";
                cout<<"First matrix \n";
                m4.show();
                cout<<"Second matrix \n";
                m5.show();
                break;

            case 3:
                cout<<"For integer matrices\n";

                    if(m1.check(m2))
                    cout<<"Addition not possible\n";
                else
                {
                cout<<"Addition\n";
                m3=m1+m2;
                m3.show();
                }

                 cout<<"\nFor float matrices\n";
                if(m4.check(m5))
                    cout<<"Addition not possible\n";
                else
                {
                cout<<"Addition\n";
                m6=m4+m5;
                m6.show();
                }
                break;

            case 4:

                    if(m1.check(m2))
                    cout<<"Subtraction not possible\n";
                else
                {
                cout<<"Subtraction\n";
                m3=m1-m2;
                m3.show();
                }
                  cout<<"\nFor float matrices\n";
                if(m4.check(m5))
                    cout<<"Subtraction not possible\n";
                else
                {
                cout<<"Subtraction\n";
                m6=m4-m5;
                m6.show();
                }
                break;

            case 5:
                cout<<"For integer matrices\n";
                cout<<"Multipli

answered question

Welcome to Stack Overflow! Please take the tour and read How to Ask. Concerning your question, it is unfortunately off-topic, because questions of this kind must be accompanied with a Minimal, Complete, and Verifiable example.

1 Answer

1

Hi I think you need to forward declare at the top of the header.

#include<iostream>
using namespace std;

template <typename T> class mat;
template <typename T> mat<T> operator+(mat<T> A,mat<T> B);

template<class T>
class mat
{
    int r,c;
    T m[5][5];
    public:
    mat(){}
    int check(mat);
    void get();
    void show();
    friend mat<T> operator+(mat<T>, mat<T>);
    mat<T> operator-(mat<T>);
    mat<T> operator*(mat<T>);
    mat<T> operator!();
};

posted this

Have an answer?

JD

Please login first before posting an answer.