[英]Using Two Linked Lists
我需要創建兩個單獨的鏈接列表,然后進行比較。 但是,當我嘗試使用構造函數的相同運算符重載創建第二個列表時,出現錯誤:
“類型為polynomial2 *的值不能用於初始化類型為polynomial *的實體”
這是我在那部分的代碼:
標頭:
#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”
在您的多項式2定義中,您的意思是說:
polynomial2 *next2;
代替
polynomial *next2;
除了暫時編譯錯誤之外,我認為您可能正在錯誤樹上。
這里的關鍵是要比較兩個本質上具有相同數據類型的列表。
我有一種強烈的感覺,您實際上並不需要兩個單獨的列表數據類型-您只想要一個。
因此,您應該真正擺脫polynomial2
並執行以下操作:
polynomial *list1;
polynomial *list2;
而且,由於您已經在使用stl,所以不使用std :: list <>的原因是什么?
struct polynomial
{
polynomial()
:coefficient(" "), power(" ") {}
polynomial(string newCoefficient, string newPower)
:coefficient(newCoefficient), power(newPower) {}
string coefficient;
string power;
};
接着
list<polynomial> list1;
如果多項式上有相等運算符,則可以比較列表是否相等。 並且可能還需要做所有其他事情。 除非分配給您,否則不允許使用std :: list,我想可能是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.