# 用单循环链表实现多项式乘法，编译出现问题

ReHall 2008-04-16 10:12:47

#include<iostream>

using namespace std;

class Term{
public:
Term(int c,int e);
Term(int c,int e, Term* nxt);
Term* InsertAfter(int c,int e);
~Term();

private:
int coef;
int exp;
friend ostream &operator<<(ostream &,const Term &);
friend class Polynominal;
};

Term::Term(int c, int e):coef(c),exp(e){link=0;};
Term::Term(int c, int e, Term *nxt):coef(c),exp(e){link=nxt;};
Term::~Term(){

};
Term* Term::InsertAfter(int c, int e)
{
};

ostream &operator<<(ostream & out,const Term& val)
{
if(val.coef==0) return out;
out<<val.coef;
switch(val.coef){
case 0:break;
case 1:out<<"X";break;
default:out<<"X^"<<val.exp;break;
}
return out;
};

class Polynominal
{
public:
Polynominal();
~Polynominal();
void Output(ostream& out)const;
Polynominal Poly_add(Polynominal& pc,int coef,int exp);
Polynominal PolyMultiply(Polynominal& r);
private:
Term* theList;
friend ostream & operator<<(ostream &,const Polynominal &);
friend istream & operator<<(istream &,Polynominal &);
friend Polynominal operator*(Polynominal &,Polynominal &);
};

Polynominal::Polynominal()
{
theList=new Term(0,-1);
};

Polynominal::~Polynominal()
while(p!=theList){
delete p;
}
delete theList;
} ;

{
Term* q=theList;
int c,e;
for(;;){
cout<<"Input a term(coef,exp):\n"<<endl;
in>>c>>e;
if(e<0)break;
q=q->InsertAfter(c,e);
}
};

void Polynominal::Output(ostream &out) const
{
int first=1;
cout<<"The Polynominal is:\n"<<endl;
if(!first &&(p->coef>0)) out<<"+";
first=0;
out<<*p;
}
cout<<"\n"<<endl;
};

Polynominal Polynominal::Poly_add(Polynominal& pc, int coef, int exp){
Term *temp=theList ; ;
Term *p1,*p2;
while(p->exp>=0){
if(p->exp<exp){
temp=temp->InsertAfter(coef,exp);
}
else if(p->exp==exp){
p->coef+=coef;
if(p->coef==0){
p1=p;
delete p1;
return pc;
}

}

};

Polynominal Polynominal::PolyMultiply(Polynominal& r)
{
int coef,exp;
Polynominal *pz=new Polynominal();

coef=(a->coef)*(b->coef);
exp=a->exp+b->exp;
}
}
return *pz;
};

ostream& operator<<(ostream & out,const Polynominal &x)
{x.Output(out);return out;
} ;
istream& operator >>(istream& in,Polynominal &x)
} ;

Polynominal& operator*(Polynominal &a,Polynominal &b)
{
Polynominal z=a.PolyMultiply(b);
return z;
};

void main()
{
Polynominal p,q,z;
cin>>p;cout<<p;
cin>>q;cout<<q;
z=p*q;cout<<z;
};

------ 已启动生成: 项目: termMU, 配置: Debug Win32 ------

termMU.cpp
d:\studio code\termmu\termmu\termmu.cpp(138) : error C2664: “Polynominal::Poly_add”: 不能将参数 1 从“Polynominal **__w64 ”转换为“Polynominal &”
d:\studio code\termmu\termmu\termmu.cpp(152) : error C2556: “Polynominal &operator *(Polynominal &,Polynominal &)”: 重载函数与“Polynominal operator *(Polynominal &,Polynominal &)”只是在返回类型上不同
d:\studio code\termmu\termmu\termmu.cpp(58) : 参见“operator *”的声明
d:\studio code\termmu\termmu\termmu.cpp(152) : error C2040: “operator *”: “Polynominal &(Polynominal &,Polynominal &)”与“Polynominal (Polynominal &,Polynominal &)”的间接寻址级别不同
d:\studio code\termmu\termmu\termmu.cpp(162) : error C2264: “operator *”: 函数定义或声明中有错误；未调用函数
d:\studio code\termmu\termmu\termmu.cpp(162) : error C2088: “*”: 对于 class 非法

termMU - 5 个错误，0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
...全文
412 12 打赏 收藏 举报

12 条回复

• 打赏
• 举报

cd1991619 2011-04-05
• 打赏
• 举报

naruto2010sasuke 2011-04-05
• 打赏
• 举报

ReHall 2008-04-21
• 打赏
• 举报

kathyflying 2008-04-21
• 打赏
• 举报

effective_person 2008-04-18
• 打赏
• 举报

``````
#include "stdafx.h"
/*

o(∩_∩)o...哈哈
*/

#include <iostream>
using namespace std;
class Term
{
public:
Term(int c,int e);
Term(int c,int e, Term* nxt);
Term* InsertAfter(int c,int e);
~Term();
private:
int coef;
int exp;
friend ostream &operator <<(ostream &,const Term &);
friend class Polynominal;
};
Term::Term(int c, int e):coef(c),exp(e){link=0;};
Term::Term(int c, int e, Term *nxt):coef(c),exp(e){link=nxt;};
Term::~Term(){ };
Term* Term::InsertAfter(int c, int e)
{
};
ostream &operator <<(ostream & out,const Term& val)
{
if(val.coef==0) return out;
out<<val.coef;
switch(val.coef)
{
case 0:
break;
case 1:
out <<"X";break;
default:
out <<"X^" <<val.exp;break;
}
return out;
};
class Polynominal
{
public:
Polynominal();
~Polynominal();
void Output(ostream& out)const;
Polynominal& Poly_add(Polynominal& pc,int coef,int exp);
Polynominal PolyMultiply(Polynominal& r);
private:
Term* theList;
friend ostream & operator <<(ostream &,const Polynominal &);
friend istream & operator <<(istream &,Polynominal &);
friend Polynominal operator*(Polynominal &,Polynominal &);
};
Polynominal::Polynominal()
{
theList=new Term(0,-1);
};

Polynominal::~Polynominal()
{
Term* p=theList;  //释放空间好好看看数据结构的书上怎么说的。再看看和你的程序有什么程序有什么不同。
//为什么你的会错
while(p!=theList)
{
delete p;
p=q;
}
} ;

{
Term* q=theList;
int c,e;
for(;;)
{
cout <<"输入系数，和指数（以<0结束）：" <<endl;
in>>c>>e;
if(e <0)
break;
q=q->InsertAfter(c,e);
}
};

void Polynominal::Output(ostream &out) const
{
int first=1;
cout <<"The Polynominal is:\n" <<endl;
{
if(!first &&(p->coef>0))
out <<"+";
first=0;
out <<*p;
}
cout <<"\n" <<endl;
};

Polynominal& Polynominal::Poly_add(Polynominal& pc, int coef, int exp)
{ //我现在没有完全弄懂你这个函数是怎么设计的。还有些逻辑的错误！自己弄弄吧！
Term *temp=pc.theList ;                                                     ;
Term *p1,*p2;
while(temp->exp>=-1)
{
if(p->exp <exp) //如果没有返回值会怎么样啊？
{
temp=temp->InsertAfter(coef,exp);
}
else if(p->exp==exp)
{
p->coef+=coef;
if(p->coef==0) /
{
p1=p;
delete p1;
}
else
{
temp=p;
}
return pc;
}
else
{
p2->coef=coef;
p2->exp=exp;
return pc;
}

}
return pc;//总体没有返回值，有些情况没有返回值

};

Polynominal Polynominal::PolyMultiply(Polynominal& r) //你的程序有一个很大的毛病。
//返回一个局部变量的引用 引用了一个已经释放掉的变量。就会报错。对你的程序看看
{
int coef,exp;
Polynominal pz;
//	Polynominal *pz=new Polynominal();
{
{
coef=(a->coef)*(b->coef);
exp=a->exp+b->exp;
}
}
return pz;
};

ostream& operator <<(ostream & out,const Polynominal &x)
{
x.Output(out);
return out;
} ;
istream& operator >>(istream& in,Polynominal &x)
{
return in;
} ;

Polynominal operator*(Polynominal &a,Polynominal &b)
{
// Polynominal z=a.PolyMultiply(b);
return a.PolyMultiply(b);
};

void main()
{
Polynominal p,q,z;
cout<<"输入第一个多项式:"<<endl;
cin>>p;
cout <<p;
cin>>q;
cout <<q;
z=p*q;
cout <<z;
};

``````

ReHall 2008-04-18
• 打赏
• 举报

ReHall 2008-04-18
• 打赏
• 举报

effective_person 2008-04-18
• 打赏
• 举报

``````
//修改了上述代码的2处提高安全性。

void Mutiply( const Polynominal& rf1, const Polynominal& rf2,Polynominal& z)
//考虑用函数，便于传递参数。
//采用引用，避免了大量的拷贝，其实也是提高效率。
{
while(q!=NULL)
{
while(p!=NULL)
{
z.Insert((*p)+(*q));
}
}
}

``````
effective_person 2008-04-18
• 打赏
• 举报

``````
/*

*/
#include "stdafx.h"
#include <iostream>
using namespace std;
namespace Poly
{
//采用命名空间实现封装数据
class  Term
{
public:
Term(){}
int coef;
int exp;
friend Term operator +( const Term& one,const Term& two);

};
Term operator +(const Term & rfone,const Term& rftwo)
{
return Term(rfone.coef*rftwo.coef,rfone.exp+rftwo.exp);
}
//多项式类
class Polynominal
{
public:
Polynominal();
~Polynominal();
void Insert(const Term& x);
friend ostream& operator <<(ostream& os,const Polynominal& rf) ;
friend istream& operator >>(istream& is,Polynominal & rf) ;
private:
Term* rearpoint;
};
Polynominal::Polynominal()
{
}
Polynominal::~Polynominal()
{
while(p!=NULL)
{
delete p;
p=q;
}
}
void Polynominal::Insert(const Term& x)
{
Term * newdata=new Term;
newdata->coef=x.coef;
newdata->exp=x.exp;
rearpoint=newdata;
}
istream& operator >>(istream & is, Polynominal & rf)
{
int coef,exp;
cout<<"以系数0 为零为结束条件"<<endl;
cout<<"系数：";
cin>>coef;
cout<<"指数:";
cin>>exp;
while(coef!=0)
{
Term *newdata=new Term;
newdata->coef=coef;
newdata->exp=exp;
rf.rearpoint=newdata;
cout<<"以系数0 为零为结束条件"<<endl;
cout<<"系数：";
is>>coef;
cout<<"指数:";
is>>exp;
}
return is;
}
ostream& operator << (ostream& os,const Polynominal& rf)
{
while(p!=NULL)
{
os<<p->coef<<"X^"<<p->exp<<"+";
}
return os;
}

void Mutiply( Polynominal& rf1, Polynominal& rf2,Polynominal& z)//考虑用函数，便于传递参数。
//采用引用，避免了大量的拷贝，其实也是提高效率。
{
while(q!=NULL)
{
while(p!=NULL)
{
z.Insert((*p)+(*q));
}
}
}

}
void main()
{
using namespace Poly;
Polynominal p,q,z;
cin>>p;
cout<<p<<endl;
cin>>q;
cout<<q<<endl;
Mutiply(p,q,z);//关键在于此，你的程序声明了太多变量了。需要很长时间赋值 ，构造。实现优化。
//还避免了，局部变量的应用问题。
cout<<endl;
cout<<"p*q=";
cout<<z<<endl;
};

``````
ReHall 2008-04-18
• 打赏
• 举报

effective_person 2008-04-16
• 打赏
• 举报

``````
Polynominal& operator*(Polynominal &a,Polynominal &b)
{
...
}

friend Polynominal operator*(Polynominal &,Polynominal &); //不一致 返回&还值啊？

``````

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-04-16 10:12

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下