Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c/c+中的链表+;_C++ - Fatal编程技术网

C++ c/c+中的链表+;

C++ c/c+中的链表+;,c++,C++,我将乘以用户必须输入的两个多项式 在第一步(从用户处获取信息)中,我遇到了以下错误: Unhandled exception at 0x00a315cb in linked polinomials.exe: 0xC0000005: Access violation writing location 0x00000000. 在我想输入多项式的另一个元素之后,我得到了这个错误 struct polynomial{ float coef ; int exp ; poly

我将乘以用户必须输入的两个多项式

在第一步(从用户处获取信息)中,我遇到了以下错误:

Unhandled exception at 0x00a315cb in linked polinomials.exe:
  0xC0000005: Access violation writing location 0x00000000.
在我想输入多项式的另一个元素之后,我得到了这个错误

struct polynomial{

    float coef ;
    int exp ;
    polynomial *next ;
} *first, *second ,*result;
while(ch!=“n”)
{
cin>>温度;
cin>>温度;
第一->系数=温度;
第一->经验=温度;

cout在第一次迭代中:

first = first->next;
您可以将
first
分配给
NULL
,因为这是
first->next
的初始设置。在分配之前,您需要为其分配空间

first->next = new polynomial;
first = first->next;

另外,是否确实要丢失指向第一个节点的指针?

您没有为整个列表分配内存。 您需要这样写:
first->next=新多项式();
第一个=第一个->下一个;


否则,您将尝试读取
NULL
地址处的内存。

相反,您应该执行以下操作:

second = new(polynomial);
first->next=second;
first=second;
在此操作之前,必须为新链接分配内存,例如

first->next = new polynomial();
只有在那之后你才能写作

first = first->next;

因为这是C++而不是C,为什么不使用一个STD::Calp+C++C++?不要用C++中的指针,至少不要用你的方式做。当你在程序中崩溃时,你首先要做的是在调试器中运行它。它会帮助你找到崩溃的位置,也让你检查变量来看看CAUS是什么。E可能是。我不知道为什么我得到了崩溃的逻辑,你的循环是错误的方式…使用<代码> do,而< /COD>循环,并创建循环内的所有节点,包括第一个节点。或者因为你使用C++,为什么不使用一个标准容器,比如,或者如果你真的想要使用列表。@ Haulasuli使用AuxI。而不是第一个
节点。
first = first->next;
first->next = new polynomial();
first = first->next;