c++, OOP,操作符重载错误

C++, OOP, operator overloading errors?

本文关键字:错误 重载 OOP c++ 操作符      更新时间:2023-10-16

当我尝试使用+*操作符为我的两个类,我得到错误。对于超载+,我得到:Invalid operands of types nod* and nod* to binary operator+.对于*和我的矩阵类中的重载操作符也是如此。

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
class nod
{
    friend class matrice;
    float re,im;
    int i,j;
    nod *next;
    public:
    void setcomp(float x, float y)
    {
        re=x;
        im=y;
    }
    void setrand(int rand)
    {
        i=rand;
    }
    void setcol(int col)
    {
        j=col;
    }
    void setnext(nod *temp)
    {
        next=temp;
    }
    float getre()
    {
        return re;
    }
    float getim()
    {
        return im;
    }
    int getrand()
    {
        return i;
    }
    int getcol()
    {
        return j;
    }
    nod *getnext()
    {
        return next;
    }
    friend istream& operator>>(istream &in, nod *z);
    friend ostream& operator<<(ostream &out, nod *z);
    nod* operator+(nod *z);
    nod* operator*(nod *z);
    void operator=(nod *z);
};
istream& operator>>(istream &in, nod *z)
{
    in>>z->re>>z->im;
    return in;
}
ostream& operator<<(ostream &out, nod *z)
{
    if(z->im<0)
    out<<z->re<<"-"<<z->im<<"i";
    else
    out<<z->re<<"+"<<z->im<<"i";
    return out;
}
nod* nod::operator+(nod *z)
{
    nod *temp;
    temp->re=re+z->re;
    temp->im=im+z->im;
    return temp;
}
nod* nod::operator*(nod *z)
{
    nod *temp;
    temp->re=(re*z->re)-(im*z->im);
    temp->im=(re*z->im)-(im*z->re);
    return temp;
}
void nod::operator=(nod *z)
{
    re=z->re;
    im=z->im;
}
class matrice
{
    nod *prim,*ultim;
    int n,m;
    public:
    matrice()
    {
        prim=NULL;
        ultim=prim;
    }
    int getl()
    {
        return n;
    }
    int getc()
    {
        return m;
    }
    nod* getprim()
    {
        return prim;
    }
    nod* getultim()
    {
        return ultim;
    }
    void adaugare(nod *p,int i,int j);
    void cautare1(nod *c, int i, int j);
    void cautare2(matrice a, nod *c, int i, int j);
    friend istream& operator>>(istream &in,matrice a);
    friend ostream& operator<<(ostream &out,matrice a);
    void afisare();
    matrice& operator+(matrice b);
    matrice& operator*(matrice b);
}temp;
void matrice::adaugare(nod *p,int i,int j)
{
    if(prim==NULL)
        {
            prim = new nod;
            prim->re=p->re;
            prim->im=p->im;
            prim->setrand(i);
            prim->setcol(j);
            prim->setnext(NULL);
            ultim = prim;
        }
        else
        {
            nod* temp = new nod;
            temp->re=p->re;
            temp->im=p->im;
            temp->setrand(i);
            temp->setcol(j);
            temp->setnext(NULL);
            ultim->setnext(temp);
            ultim = temp;
        }
}
istream& operator>>(istream& in,matrice a)
{
    float x,y;
    int i,j;
    nod *p;
    p=new nod;
    in>>a.n>>a.m;
    for(i=1;i<=a.n;i++)
    {
        for(j=1;j<=a.m;j++)
        {
            in>>p;
            a.adaugare(p,i,j);
        }
    }
    return in;
}
ostream& operator<<(ostream &out,matrice a)
{
    nod *p=a.getprim();
    int i,j;
    for(i=1;i<=a.n;i++)
    {
        for(j=1;j<=a.m;j++)
        {
            out<<p;
        }
        out<<endl;
    }
    return out;
}
void matrice::cautare1(nod *c, int i, int j)
{
    c=prim;
    while(c!=NULL)
    {
        if((c->getrand()==i)&&(c->getcol()==j))
        break;
        c=c->getnext();
    }
}
void cautare2(matrice a, nod *c, int i, int j)
{
    c=a.getprim();
    while(c!=NULL)
    {
        if((c->getrand()==i)&&(c->getcol()==j))
        break;
        c=c->getnext();
    }
}
void matrice::afisare()
{
    int i,j;
    nod *p=prim;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            cout<<p;
            p=prim->getnext();
        }
    }
}
matrice& matrice::operator+(matrice b)
{
    nod *prim2=b.prim;
    nod *p1=prim, *p2=prim2, *ps;
    ps=new nod;
    int i,j;
    matrice s;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            ps=p1+p2;
            s.adaugare(ps,i,j);
            p1=p1->getnext();
            p2=p2->getnext();
        }
    }
    return s;
}
matrice& matrice::operator *(matrice b)
{
    matrice p;
    nod *p1=new nod,*p2=new nod,*q=new nod;
    int i,j,k;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=b.m;j++)
        {
            q->setcomp(0,0);
            q->setrand(i);
            q->setcol(j);
            p.adaugare(q,i,j);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=b.m;j++)
        {
            for(k=1;k<=m;k++)
            {
                cautare2(p,q,i,j);
                cautare1(p1,i,k);
                cautare2(b,p2,k,j);
                q=p1*p2;
            }
        }
    }
    return p;
}
void submat(matrice a, matrice &b,int n, int x, int y)
{
    int i,j,k=0;
    nod *q;
    for(i=1;i<=n;i++)
        for(j=1;j<=n;j++)
            if((i!=x)&&(j!=y))
            {
                k++;
                if(k==n)
                {
                    k=1;
                    x++;
                }
                cautare2(a,q,i,j);
                b.adaugare(q,x,k);
            }
}
nod *determinant(matrice a,int n)
{
    matrice b;
    nod *temp,*c,*d,*oarecare,*p1,*p2,*p3,*p4;
    int i;
    float real,imag,real2,imag2;
    temp=new nod;
    temp->setcomp(0.0,0.0);
    temp->setrand(0);
    temp->setcol(0);
    if(n==1)
    {
        cautare2(a,oarecare,1,1);
        temp->setcomp(oarecare->getre(),oarecare->getim());
    }
    else
    {
        if(n==2)
        {
            cautare2(a,p1,1,1);
            cautare2(a,p2,2,2);
            temp=p1*p2;
            cautare2(a,p3,1,2);
            cautare2(a,p4,2,1);
            temp=temp-p3*p4;
        }
            else
            {
                for(i=1;i<=n;i++)
                {
                    submat(a,b,n,1,i);
                    c->setcomp(pow(-1,i+1),0);
                    c->setrand(0);
                    c->setcol(0);
                    cautare2(a,p1,1,i);
                    c=c*p1;
                    d=temp;
                    c=c*determinant(b,n-1);
                    temp=d+c;
                }
            }
    }
    return temp;
}
void inversa(matrice a, nod *&z)
{
    nod *p1,*p2,*temp,*y;
    matrice b,tr,c;
    int i,j,n;
    n=(a.getultim())->getrand();
    float real, imag;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cautare2(a,p1,j,i);
            tr.adaugare(p1,i,j);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            submat(tr,b,n,i,j);
            y->setcomp(pow(-1,i+j),0);
            c.adaugare(y,i,j);
            temp=determinant(b,n-1);
            cautare2(c,p2,i,j);
            p2=p2*temp;
            real=z->getre()/(pow(z->getre(),2)+pow(z->getim(),2));
            imag=z->getim()/(pow(z->getre(),2)+pow(z->getim(),2));
            temp->setcomp(real, imag);
            p2=p2*temp;
        }
    }
    c.afisare();
}
int main()
{
    ifstream f("mat.txt");
    matrice a,b;
    f>>a>>b;
    cout<<"Matricea A:n"<<a;
    cout<<"nMatricea B:n"<<b;
    cout<<a+b;
    return 0;
}

首先你的代码:

nod* nod::operator+(nod *z)
{
    nod *temp;
    temp->re=re+z->re;
    temp->im=im+z->im;
    return temp;
}
  1. nod *temp是错误的,temp不是一个对象,只是一个没有分配内存的指针。
  2. 当重载操作符+时需要返回一个新对象,而不是指针。
  3. operator+的形参应该是对对象的引用,而不是指向对象的指针
考虑到这一点,我将重写代码如下:
nod nod::operator+(const nod &z)
{
    nod temp;
    temp.re=this->re+z.re;
    temp.im=this->im+z.im;
    return temp;
}
下面是一些很好的例子:http://www.cprogramming.com/tutorial/operator_overloading.html