我正在处理一些使用链接列表的代码,而且我很难确定为什么我的+运算符无法正常工作,我不断收到上述错误。
我错过了什么?
#include <iostream>
#include <string>
using namespace std;
struct sortedListNode
{
char letter;
int occurrences = 1;
sortedListNode *next;
};
sortedListNode *operator+(sortedListNode *lhs, sortedListNode *rhs)
{
int i = 0;
sortedListNode *head, *tail, *curr, *prev, *tempLoc;
sortedListNode *list1, *list1curr, *list2, *list2curr;
list1 = lhs;
list2 = rhs;
// Copy list1 into output list
head = new sortedListNode;
head->letter = list1->letter;
head->occurrences = list1->occurrences;
head->next = NULL;
tail = head;
list1curr = list1;
list1curr = list1curr->next;
while (list1curr != NULL)
{
tempLoc = new sortedListNode;
tempLoc->letter = list1curr->letter;
tempLoc->occurrences = list1curr->occurrences;
tempLoc->next = NULL;
tail->next = tempLoc;
tail = tempLoc;
list1curr = list1curr->next;
}
curr = head;
while (list2curr != NULL)
{
while (curr != NULL)
{
if (curr->letter == list2curr->letter)
{
curr->occurrences++;
break;
}
else if ((curr->letter > list2curr->letter) && (curr == head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = list2curr->letter;
head = tempLoc;
break;
}
else if ((curr->letter > list2curr->letter) && (curr != head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = list2curr->letter;
prev->next = tempLoc;
break;
}
else if ((curr == tail) && (curr->letter < list2curr->letter))
{
tempLoc = new sortedListNode;
tempLoc->next = NULL;
tempLoc->letter = list2curr->letter;
tail->next = tempLoc;
tail = tempLoc;
break;
}
prev = curr;
curr = curr->next;
}
curr = head;
list2curr = list2curr->next;
}
return head;
}
sortedListNode *fromString(string inWord)
{
int i = 0;
sortedListNode *head, *tail, *curr, *prev, *tempLoc;
// Put the first letter in as the first element, set head and
// tail to this element.
head = new sortedListNode;
tail = head;
head->letter = inWord[0];
head->next = NULL;
curr = head;
for (int i = 1; inWord[i] != '\0'; i++)
{
while (curr != NULL)
{
if (curr->letter == inWord[i])
{
curr->occurrences++;
break;
}
else if ((curr->letter > inWord[i]) && (curr == head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = inWord[i];
head = tempLoc;
break;
}
else if ((curr->letter > inWord[i]) && (curr != head))
{
tempLoc = new sortedListNode;
tempLoc->next = curr;
tempLoc->letter = inWord[i];
prev->next = tempLoc;
break;
}
else if ((curr == tail) && (curr->letter < inWord[i]))
{
tempLoc = new sortedListNode;
tempLoc->next = NULL;
tempLoc->letter = inWord[i];
tail->next = tempLoc;
tail = tempLoc;
break;
}
prev = curr;
curr = curr->next;
}
curr = head;
}
return head;
}
void printList(sortedListNode *inSortedListNode)
{
sortedListNode *curr;
curr = inSortedListNode;
int nodeCounter = 0;
while (curr != NULL)
{
nodeCounter++;
cout << "Node " << nodeCounter << " at " << curr << " - Letter = " << curr->letter << ", Occurrences = " << curr->occurrences << ", Next Node = "
<< curr->next << endl;
curr = curr->next;
}
}
int main()
{
string word1, word2;
sortedListNode *list1;
sortedListNode *list2;
sortedListNode *list3;
cout << "Enter first word: ";
cin >> word1;
cout << "Enter second word: ";
cin >> word2;
list1 = fromString(word1);
cout << "Letter list from word one: " << endl;
printList(list1);
list2 = fromString(word2);
cout << "Letter list from word two: " << endl;
printList(list2);
list3 = list1 + list2;
cout << "Letter list from both words: " << endl;
printList(list3);
cin.ignore(32767, '\n');
char dummy[1]{};
cin.getline(dummy, 1);
}
请注意我知道运营商定义中存在错误,因为我还没有写完。我试图超越将两个链表的sortedListNode对象作为参数的障碍。
这是作业。主要是由教师提供的存根主体。 operator +应该采用两个列表并将它们合并在一起,然后输出合并列表。
答案 0 :(得分:2)
错误消息的直接原因是您没有调用已定义的operator+
。这是因为您已将operator+
定义为将两个sortedListNode
作为参数,但您已经传入了两个指针。如果你改变你的运算符而不是指针(sortedListNode*
),它将超过立即的语法错误。
然而,那是你真正的问题开始的地方。您对operator +的实现也有问题。它返回head
,它不是sortedListNode
结构的一部分。至关重要的是,它不会使用您传入其中的两个列表中的任何一个。
编辑:现在我们可以看到完整的代码,感谢发布。
我会回应沃尔特所说的:添加指针毫无意义。相反,您应该定义一个运算符+来一起添加列表:
sortedNodeList operator+(const sortedNodeList& lhs, const sortedNodeList &rhs){
...
}
换句话说,你的参数应该是对列表(而不是指针)的引用,你的结果应该是一个列表,而不是一个指针。
答案 1 :(得分:1)
对于
sortedListNode *list1, *list2, *list3;
list3 = list1 + list2;
你需要
sortedListNode *operator+(sortedListNode*lhs, sortedListNode*rhs);
但你只提供
sortedListNode *operator+(sortedListNode lhs, sortedListNode rhs);
我还应该说,为指针类型重载+
运算符是没有意义的(即使它被允许)。