这是我的头文件
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream>
#include <string>
using namespace std;
class Node
{
friend class LinkedList;
public:
Node(string& name, int num) :studentName(name), RUID(num)
{
this->next = NULL;
}
private:
string studentName;
int RUID;
Node *next;
};
class LinkedList
{
public:
LinkedList();
~LinkedList();
LinkedList& operator+(LinkedList &i);
//LinkedList operator=();
void makeLists(int n);
void addNode(LinkedList &i);
void removeNode();
void printList();
void printElement();
void sortList();
private:
Node *head;
Node *tail;
int size;
};
#endif
...这是我的操作员+功能
LinkedList& LinkedList::operator+(LinkedList &i)
{
LinkedList tohma;
tohma = *this;
tohma += i;
return tohma;
}
我收到了一条带有+ =运算符的错误消息,但我对如何以不同方式执行此操作感到困惑。我觉得我很接近,但也许我犯了一个逻辑错误?
任何和所有帮助将不胜感激
答案 0 :(得分:0)
通常,operator+
的{{1}}结构如下:
LinkedList
对于具有类似逻辑的LinkedList operator+(const LinkedList& _list) {
// ptr to list to return, initialised with this
LinkedList* ret_list = this;
// temporary node ptr for iterating through _list
Node* temp = _list.head;
// loop through contents of _list
while (temp != nullptr) {
// insert data from _list to ret_list
ret_list->insert(temp->data);
// ^= this will need to be changed for your specific list fields and insert methods
temp = temp->next;
}
return &ret_list;
}
:
operator+=
我可能在这里遇到了一些错误,因为它已经很晚了,但它应该是准确的。
答案 1 :(得分:0)
LinkedList LinkedList::operator+(const LinkedList &i)
{
LinkedList* tohma = this;
Node* temp = i.head;
tohma->tail->next = temp;
return *tohma;
}
因为我已经为我的第一个列表保存了尾部节点,所以我能够使用它将它与第二个列表连接起来