gpt4 book ai didi

c++ - 用于添加两个双向链表的重载运算符

转载 作者:行者123 更新时间:2023-11-28 02:38:26 25 4
gpt4 key购买 nike

我需要有关重载“+”运算符以将两个双向链表相加的帮助。由于出现“不匹配 operator=...”错误,我无法编译我的程序。我已经重载了 '=' 运算符,但很难将添加的结果打印到标准输出中。我还重载了 << 运算符。几个小时以来一直试图找出问题所在,但没有成功。非常欢迎任何有关如何解决此问题和/或解决方案的提示。这是我的 OOP 类(class)的作业。提前致谢!

编辑:代码背后的基本思想是复制集。重载运算符“+”应作为 union 使用,而“*”应作为交集使用。我努力将 union 正确打印到标准输出。 '+=' 似乎工作正常。 '<<' 也很好用,但仅在打印单个列表时有效。


编辑:编译器产生的错误(g++,code::blocks 的输出,我删除了编译器注释):

llist3.cpp|149|error: no match for ‘operator=’ (operand types are ‘LList’ and ‘LList’)|
llist3.cpp|106|note: no known conversion for argument 1 from ‘LList’ to ‘LList&’|
llist3.cpp|151|error: no match for ‘operator=’ (operand types are ‘LList’ and ‘LList’)|
llist3.cpp|106|note: no known conversion for argument 1 from ‘LList’ to ‘LList&’|
llist3.cpp|152|error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘LList’)|

   #include<iostream>
using namespace std;

class LList {
public:
struct Node {
int elem;
Node* succ;
Node* prev;
Node() : succ(0), prev(0), elem(0) {}
};
LList();
LList(LList& list);
~LList();

Node* next();
Node* begin() { curr = head; }

int getElem() { return curr->elem; }
void addElem(int elem);
LList operator+(LList& set);
LList operator+(int elem);
LList& operator+=(LList& set);
LList& operator+=(int elem);
LList& operator=(LList& list);
friend ostream& operator<<(ostream& os, LList& obj);
private:
Node* curr;
Node* head;
Node* tail;
int size;
void pushFront(Node* n);
void pushInside(Node* n);
void pushBack(Node* n);
};

LList::LList() : head(0), tail(0), size(0), curr(0) {}
LList::LList(LList& list) : size(0), curr(0), head(0), tail(0) {
list.curr = list.head;
while(list.curr) {
addElem(list.getElem());
list.next();
}
}
LList::Node* LList::next() {
if (curr)
return (curr = curr->succ);
else
return 0;
}
void LList::addElem(int elem) {
Node* n = new Node;
n->elem = elem;
if (curr) {
if (curr == head && elem < curr->elem) {
pushFront(n);
}
else if (elem > curr->elem) {
curr = curr->succ;
addElem(elem);
}
else if (elem < curr->elem && elem > (curr->prev)->elem) {
pushInside(n);
}
else if (elem < curr->elem) {
curr = curr->prev;
addElem(elem);
}
} else {
pushBack(n);
}
}
void LList::pushFront(Node* n) {
head = n;
n->succ = curr;
curr->prev = n;
n->prev = 0;
curr = n;
size++;
}
void LList::pushInside(Node* n) {
(curr->prev)->succ = n;
n->succ = curr;
n->prev = curr->prev;
curr->prev = n;
size++;
}
void LList::pushBack(Node* n) {
if (!head) {
head = n;
} else {
tail->succ = n;
n->prev = tail;
}
tail = n;
curr = n;
size++;
}
LList::~LList() {
for (curr = head; curr;) {
Node* temp = curr->succ;
delete curr;
curr = temp;
}
}
LList& LList::operator=(LList& list) {
list.begin();
if (this != &list) {
for (curr = head; curr;) {
Node* temp = curr->succ;
delete curr;
curr = temp;
}
while (list.curr) {
addElem(list.getElem());
list.next();
}
}
return *this;
}
ostream& operator<<(ostream& os, LList& list) {
LList::Node* p = list.head;
os << "{ ";
while(p) {
os << p->elem << (p->succ ? ", " : "");
p = p->succ;
}
os << " }" << endl;
return os;
}
LList LList::operator+(LList& set) {
LList temp = *this;
temp += set;
return temp;
}
LList LList::operator+(int elem) {
*this += elem;
return *this;
}
int main() {
LList setA;
setA.addElem(1234);
setA.addElem(1435);
setA.addElem(1100);
LList setB;
setB.addElem(1234);
setB.addElem(1435);
setB.addElem(5100);
setB = setA + 1234; // 1st error here
LList setD;
setD = setA + setB; //2nd
cout << setA + setB << endl; //3rd
}

最佳答案

您的代码中有一个明显的错误:

Node* begin()  { curr = head; }

此代码调用未定义的行为,因为您没有返回值。应该是这样的:

Node* begin()  { curr = head; return curr; }

此外,您应该传递您的 LList通过不改变 LList 的函数中的 const 引用参数:

例如:

LList::LList(LList& list);
LList& operator=(LList& list);
friend ostream& operator<<(ostream& os, LList& obj);

应该是:

LList::LList(const LList& list);
LList& operator=(const LList& list);
friend ostream& operator<<(ostream& os, const LList& obj);

请更改这些和其他函数以传递 const 引用。如果您想了解为什么要更改此设置,那么如果您尝试这样做,您会立即看到问题:

LList list1;
LList list2;
//...
std::cout << list1 + list2;

operator <<正在寻找非常量 LList对象,但添加“内联”返回临时 LList (这意味着返回值将为 const )。由于您的重载,代码将无法编译 operator <<只接受非常量 LList .

因此您需要更改operator << 中的参数到 const LList& .

关于c++ - 用于添加两个双向链表的重载运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com