一元多项式相加打代码运行出现bug
# include <iostream>(严重性 代码 说明 项目 文件 行 禁止显示状态错误 LNK1120 1 个无法解析的外部命令) (严重性 代码 说明 项目 文件 行 禁止显示状态
错误 LNK2019 无法解析的外部符号 "public: __thiscall Polynomial::~Polynomial(void)" (??1Polynomial@@QAE@XZ),函数 _main 中引用了该符号)
using namespace std;
struct Node //定义多项式单链表的结点
{
int coef, exp; //coef表示系数,exp表示指数
Node* next;
};
class Polynomial
{
public:
Polynomial(); //构造函数
Polynomial(const Polynomial& B); //拷贝构造函数
~Polynomial(); //析构函数,同单链表析构函数
Polynomial operator+(Polynomial& B); //重载运算符,多项式相加
void Print(); //打印一元多项式单链表
private:
Node* first; //一元多项式单链表的头指针
};
Polynomial:: Polynomial()
{
Node* r = nullptr, * s = nullptr;
int coef, exp;
first = new Node; //申请头结点
r = first; //尾插法建立单链表
cout << "请输入系数和指数:";
cin >> coef >> exp; //输入第一项的系数和指数
while (coef != 0) //循环结束的条件是输入系数为0
{
s = new Node; s->coef = coef; s->exp = exp;
r->next = s; r = s; //将结点s插入单链表的尾部
cout << "请输入系数和指数:";
cin >> coef >> exp;
}
r->next = nullptr;
}
Polynomial::Polynomial(const Polynomial& B)
{
first = B.first;
}
Polynomial Polynomial ::operator+(Polynomial & B)
{
Node* pre = first, * p = pre->next; //工作指针pEe和p初始化
Node* qre = B.first, * q = qre->next; ///工作指针可xe和q初始化
Node* qtemp = nullptr;
while (p != nullptr && q != nullptr)
{
if (p->exp < q->exp) { //第1种情况
pre = p; p = p->next;
}
else if (p->exp > q->exp) { //第2种情况
qtemp = q->next;
pre->next = q; //将结点q插入到结点p之前
q->next = p;
pre = q;
q = qtemp;
qre->next = q;
}
else { //第3种情况
p->coef = p->coef + q->coef;
if (p->coef == 0) { //系数相加为0,则删除结点p
pre->next = p->next;
delete p;
p = pre->next;
}
else { //系数不为0
pre = p; p = p->next;
}
qre->next = q->next; //第3种情况都要删除结点4
delete q;
q = qre->next;
}
}
if (q != nullptr) pre->next = q; //将结点q链接在第一个单链表的后面
return *this;
}
void Polynomial::Print()
{
Node* p = first->next;
if (p != nullptr) //输出第一项
{
cout << p->coef << "x" << p->exp;
p = p->next;
}
while (p != nullptr)
{
if (p->coef > 0) //输出系数的正号或负号
cout << "+" << p->coef << "x" << p->exp;
else
cout << p->coef << "x" << p->exp;
p = p->next;
}
}
int main()
{
Polynomial A{}, B{}, C{}; //定义多项式A和B
A.Print(); B.Print();
C = A + B; //+运算符重载,对象赋值调用拷贝构造函数
cout << "结果是:";
C.Print(); //输出相加结果
return 0;
} 太晚了,你的程序太过复杂,我给你一个《C语言常见错误英中对照及分析》=https://fishc.com.cn/thread-180650-1-1.html,你自己去对照错误信息查找错误所在吧 析构函数只有声明,没有定义
页:
[1]