代码中没有错误,但是当我运行程序时,我得到“多项式.exe已停止工作”.错误
No errors in code, but when i run the program I get the "Polynomial.exe has stopped working" error
我有这个任务,我应该使用链表来存储多项式并对它们执行基本的数学函数。我编写了接受和打印多项式的代码。Eclipse没有显示任何错误,程序正常运行并接受系数,但之后它只是停止工作,并弹出一个对话框说"多项式。exe已停止工作"。下面是代码:
#include<iostream>
using namespace std;
class term
{
private:
int coef;
int pow;
term *next;
friend class polynomial;
public:
term(int a,int b)
{
coef=a;
pow=b;
next=NULL;
}
};
class polynomial
{
private:
term *head;
public:
polynomial()
{
head=NULL;
}
void ini()
{
int deg;
cout<<"Enter Degree of Polynomial";
cin>>deg;
head=create(deg);
}
term *create(int deg)
{
int coeff;
cout<<"Enter Coefficient for the term x^"<<deg;
cin>>coeff;
term q=term(coeff,deg);
if(deg==0)
{
q.next=NULL;
}
else
{
q.next=create(deg-1);
}
return &q;
}
void pri()
{
term *temp;
for(temp=head;temp->next!=NULL;temp=temp->next)
{
cout<<temp->coef;
}
}
};
int main()
{
polynomial a=polynomial();
a.ini();
a.pri();
return 0;
}
谁能告诉我为什么会这样? 每次调用term *create(int deg)
时,都获得一个悬空指针,因为它返回一个局部变量的地址。被寻址的局部变量在函数返回后立即消失。任何对返回指针的解引用都会调用未定义行为。
下面是所讨论的成员函数的简化版本:
term *create(int deg)
{
term q=term(coeff,deg); // local variable
return &q; // Oops!
}
参见局部变量的内存可以在其作用域之外访问吗?
通过将分配/存储与程序逻辑分离,可以极大地简化代码。例如,您可以使用std::vector
在多项式类中存储项。您还应该将多项式类中系数值的读入和打印分离。例如,
class polinomial
{
// construct polinomial of a certain order
explicit polinomial(size_t order) : coeffs(order) {}
// set coefficient of term of degree deg
// return false if deg beyond order
bool set_coeff(size_t deg, int coeff)
{
if (coeffs.size() < deg) return false;
coeffs[deg] = coeff;
}
// obtain the coefficient of a term of degree deg
int coeff(size_t deg) const { return coeffs[deg]; }
private:
std::vector<int> coeffs;
};
然后实现单独的函数或运算符来读取和打印系数
你可以尝试动态创建变量,就像这样:
class term
{
private:
int coef;
int pow;
term *next;
friend class polynomial;
public:
term(int a,int b)
{
coef=a;
pow=b;
next=NULL;
}
};
class polynomial
{
private:
term* head;
public:
polynomial()
{
head=NULL;
}
~polynomial()
{
if(!!head)
{
delete head;
head=NULL;
}
}
void ini()
{
int deg;
cout<<"Enter Degree of Polynomial";
cin>>deg;
head=create(deg);
}
term* create(int deg)
{
int coeff;
cout<<"Enter Coefficient for the term x^"<<deg;
cin>>coeff;
term *q= new term(coeff,deg);
if(deg==0)
{
q->next=NULL;
}
else
{
q->next=create(deg-1);
}
return q;
}
void pri()
{
term *temp;
for(temp=head;temp->next!=NULL;temp=temp->next)
{
cout<<temp->coef;
}
}
};
int main()
{
polynomial a=polynomial();
a.ini();
a.pri();
return 0;
}
相关文章:
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 终端不会为C++文件创建.exe文件吗
- 在Windows中以.exe的形式运行c++
- c++模板来表示多项式
- Visual Studio mkl_link_tool.exe链接错误
- PrintWindow函数在notepad.exe中出错
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 获取当前正在运行的 exe 名称(不是路径)
- 从 exe 文件 (Visual Studio ) 启动时调试断言失败
- 1d.exe找不到SFML文件?
- VI工作室代码错误无法打开输出文件主.exe
- 如何为每个客户生成自定义 exe
- 使用参数在 C# 的C++上运行.exe
- 在 ******.exe 中0x00000000772CA267 (ntdll.dll) 处未处理的异常:0xC0000
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 解析引用 |exe/dll vs lib
- Q没有管理权限的 exe 无法启动维护工具
- 如何定义自定义生成配置类型,其中通常是.exe的目标改为 DLL
- 代码中没有错误,但是当我运行程序时,我得到“多项式.exe已停止工作”.错误