我的一些操作员有问题。运算符在我的搜索和deleteWord中。在搜索中,我试图检查列表中的单词是否与搜索单词匹配。在deleteWord中,我试图检查要删除的单词是否与列表中的单词完全匹配,然后将其删除。
我一直在寻找如何修复操作员的方法,但我不理解。先感谢您。
一些错误:
错误1错误C2784:'bool std :: opeator> =(const std :: basic_string <_Elem,_Traits,_Alloc>&,const_Elem *):无法为'const LinkedList'推断'const_Elem *'的模板参数
错误2错误C2784:'bool std :: operator> =(const _Elem *,const std :: basic_string <_Elem,_Traits,_Alloc>&)':无法从'std ::推断出'const _Elem *'的模板参数细绳'
错误3错误C2784:'bool std :: operator> =(const std :: basic_string <_Elem,_Traits,_Alloc>&,const std :: basic_string <_Elem,_Traits,_Alloc>&)':无法推导以下模板参数'const std :: basic_string <_Elem,_Traits,_Alloc>&'来自'const LinkedList'
错误29错误C2678:二进制'==':未找到采用'std :: string'类型的左操作数的运算符(或没有可接受的转换)
错误59 IntelliSense:没有运算符“ ==”与这些操作数匹配
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
#include <iostream>
using namespace std;
//Definition of the node
struct linkedList{ //linked list
string word; //node holds string word
linkedList *next; //points to next node
linkedList *prev; //points to prev node
};
class LinkedList{
public:
LinkedList(); //default const
~LinkedList(); //destructor
const LinkedList& operator = (const LinkedList &); //overload the assignment operator
void initializeList(); //func to init list to an empty state
void read();
void printForward(linkedList *firstWord);
void printBackward(linkedList *lastWord);
void insert();
bool search(const LinkedList& searchItem) const;
void deleteWord(const LinkedList& deleteItem);
void clear();
protected:
int count;
linkedList *firstWord; //pointer to the first node
linkedList *lastWord; //pointer to the last node
};
LinkedList::LinkedList()
{
firstWord = NULL;
lastWord = NULL;
count = 0;
}
...
... //more code
...
bool LinkedList::search(const LinkedList& searchItem) const
{
bool found = false;
linkedList *temp; //pointer to traverse list
temp = firstWord;
while (temp != NULL && !found)
if (temp->word >= searchItem)
found = true;
else
temp = temp->next;
if (found)
found = (temp->word == searchItem); //test for equality
return found;
}
void LinkedList::deleteWord(const LinkedList& deleteItem)
{
linkedList *temp; //pointer to traverse the list
linkedList *trailTemp; ///pointer just before temp
bool found;
if (firstWord == NULL)
cout << "Cannot delete from an empty list." << endl;
else if (firstWord->word == deleteWord){ //node to be deleted is the firstWord
temp = firstWord;
firstWord = firstWord->next;
if (firstWord != NULL)
firstWord->prev = NULL;
else
lastWord = NULL;
count--;
delete temp;
}
else{
found = false;
temp = firstWord;
while (temp !=NULL && !found) //search the list
if (temp->word >= deleteWord)
found = true;
else
temp = temp->next;
if (temp == NULL)
cout << "The word to be deleted is not in the list." << endl;
else if (temp->word == deleteWord){ //check for equality
trailTemp = temp->prev;
trailTemp->next = temp->next;
if (temp->next != NULL)
temp->next->prev = trailTemp;
if (temp == lastWord)
lastWord = trailTemp;
count--;
delete temp;
}
else
cout << "The word to be deleted is not in the list." << endl;
}
}
...
... //more code
...
#endif
这行:
if (temp->word >= searchItem)
正在将astd::string
与进行比较LinkedList
。您想将word
intemp
与一个字符串进行比较。
将该函数更改为:
bool LinkedList::search(const std::string& searchItem) const
您的其他大多数错误都是该主题的变体。
编辑:误把LinkedList
的linkedList
-请不要有一个结构与类别名称相同,但不同的情况。这很令人困惑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句