在C ++中使用模板和运算符重载执行矩阵运算

时间:2018-11-03 13:34:55

标签: c++ templates operator-overloading friend

这是我的完整代码,其中我使用模板和运算符重载对矩阵执行了加,减和换位运算。 当我使用成员功能时,一切都很好。但是,当我尝试使用下面的朋友功能进行操作时,将显示错误(稍后说明)。 有人可以帮我纠正此代码。

#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<<"Multiplication\n";
                m3=m1*m2;
                m3.show();

                cout<<"\nFor float matrices\n";
                cout<<"Multiplication\n";
                m6=m4*m5;
                m6.show();

                break;

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

                 cout<<"Transpose of first matrix\n";
                m3=!m1;
                m3.show();
                cout<<"Transpose of second matrix\n";
                m3=!m2;
                m3.show();

                  cout<<"\nFor float matrices\n";
                cout<<"Transpose of first matrix\n";
                m6=!m4;
                m6.show();
                cout<<"Transpose of second matrix\n";
                m6=!m5;
                m6.show();
                break;

            case 7:
                break;
            default:
                cout<<"Please enter a valid choice\n";
        }
    }while(ch!=7);
    return 0;
}

现在,我在输出中得到什么
警告:朋友声明'mat operator +(mat,mat)'声明一个非模板函数[-Wnon-template-friend]
注意:(如果这不是您想要的,请确保已声明功能模板,并在此处在功能名称后添加<>)
错误:未定义对operator +(mat,mat)的引用
错误:未定义对operator +(mat,mat)的引用

1 个答案:

答案 0 :(得分:0)

实例化mat<int>mat<float>时,朋友声明被绑定到intfloat上。 然后,按照定义

template<class T>
mat<T> operator+(mat<T> A,mat<T> B)
{
    mat<T> C;
    C.r=A.r; C.c=A.c;
    ...
}

与这些声明没有关系,您会收到链接器错误。

您必须将friend声明为功能模板。也就是说,我们应该重写以下行

friend mat<T> operator+(mat<T>, mat<T>);

如下:

template<class U> friend mat<U> operator+(mat<U>, mat<U>);