c++, OOP,操作符重载错误
C++, OOP, operator overloading errors?
当我尝试使用+
和*
操作符为我的两个类,我得到错误。对于超载+
,我得到: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;
}
- nod *temp是错误的,temp不是一个对象,只是一个没有分配内存的指针。
- 当重载操作符+时需要返回一个新对象,而不是指针。
- 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
相关文章:
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 在运算符重载定义中使用成员函数(const错误)
- 重载方法的方式会在使用临时调用时生成编译器错误
- 数组索引重载错误
- C++:需要帮助了解运算符重载错误
- 在类Bat代码中,这给了我错误:重载的"Bat()"的调用是不明确的Bat(;)
- 不明确的错误重载运算符<<QdataStream 子类和个人类
- 具有相同参数的不同模板模板参数的错误重载函数
- 编译错误:重载函数的多个实例与 arument 列表匹配
- clang-libc++错误:重载解析选择了隐式删除的复制赋值运算符
- 错误:重载的调用不明确
- C++ 编译器选择输出流运算符<<的错误重载
- 分段错误重载运算符<<
- 错误:重载函数的多个实例与参数列表匹配
- 错误:重载的“max(int, int)”的调用不明确
- C++编译器选择类成员函数的错误重载
- 编译错误:重载操作符()
- 错误:重载的“bind(int(Class::*)(int,int),Class*,int,int”的调用不明确
- 未解析的外部符号错误重载操作符+模板
- 错误重载在构建"OpenSubdiv"时具有类似的转换