c++ - 使用两个链表

标签 c++ pointers error-handling operator-overloading

我需要创建两个单独的链表,然后比较它们。但是,当我尝试为构造函数创建具有相同运算符重载的第二个列表时,出现错误:

“多项式2*类型的值不能用于初始化多项式*类型的实体”

这是我的代码:

标题:

#include <iostream>
#include <string>

using namespace std; 

struct polynomial
{ 
    polynomial();
    polynomial(string newCoefficient, string newPower, polynomial *nextPtr);
    string coefficient;
    string power;
    polynomial *next; 
};

struct polynomial2
{ 
    polynomial2();
    polynomial2(string newCoefficient2, string newPower2, polynomial2 *nextPtr2);
    string coefficient2;
    string power2;
    polynomial *next2; 
};

class linkedList
{
public:
    linkedList();
    void callFunctions();
private:
    polynomial *head;
    polynomial2 *head2;
    void makeList(polynomial *head, polynomial2 *head2);
    void showList(polynomial *head);
    void compareNodes(polynomial *head, polynomial2 *head2);
};

#endif
/* defined(__Assignment3__Polynomial__) */

.CPP代码:

linkedList::linkedList()
{
    head = 0; 
};

polynomial::polynomial()
{
    coefficient = " "; 
    power = " "; 
    next = NULL;
};

polynomial2::polynomial2()
{
    coefficient2 = " "; 
    power2 = " "; 
    next2 = NULL
};

polynomial::polynomial(string newCoefficient, string newPower, polynomial *nextPtr )
    :
coefficient(newCoefficient),
    power(newPower), 
    next(nextPtr) 

{}

polynomial2::polynomial2(string newCoefficient2, string newPower2, polynomial2 *nextPtr2)
    :
coefficient2(newCoefficient2),
    power2(newPower2), 
    next2(nextPtr2)

{}

错误出现在 .cpp 文件的最后一行“next2(nextPtr2)”。 “nextPtr2”带下划线

最佳答案

在您的 polynomial2 定义中,您的意思是:

polynomial2 *next2;

代替

polynomial *next2;

关于c++ - 使用两个链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887477/

相关文章:

c++ - 使用 std::deque 和 clang 编译器

php - 无法修改 header 信息-尽管没有错误也无法解决

c++ - 将命令行实用程序的输出传递给编译器/链接器

c - C 中的字符串解析、存储和读取

c - 使用 strcmp 比较字符指针和文字

c - 本地指针, `static` 指针和 `malloc` 指针

error-handling - 为什么 li3 HTML 助手有时不能生成正确的路径?

java - 在整个 Java 应用程序中捕获异常

c++ - QT QNetworkAccessManager 和信号

c++ - cldoc 在模板部分特化时崩溃