Ошибка оператора в Visual Studio (ошибка C2784)

У меня проблема с некоторыми моими операторами. Операторы в моем поиске и удалении слов. В поиске я пытаюсь проверить, соответствует ли слово в моем списке поисковому слову. В deleteWord я пытаюсь проверить, что слово, которое я хочу удалить, идеально совпадает со словом в моем списке, а затем удалить его.

Я посмотрел, как исправить мой оператор, но я не понимаю. Заранее спасибо.

Некоторые ошибки:

Ошибка 1 ошибка C2784: ‘bool std :: opeator> = (const std :: basic_string<_Elem, _Traits, _Alloc> &, const_Elem *): не удалось вывести аргумент шаблона для ‘const_Elem *’ для ‘const LinkedList’

Ошибка 2 ошибка C2784: ‘bool std :: operator> = (const _Elem *, const std :: basic_string<_Elem, _Traits, _Alloc> &) ‘: не удалось вывести аргумент шаблона для’ const _Elem * ‘из’ std :: string ‘

Ошибка 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

-2

Решение

Эта строка:

if (temp->word >= searchItem)

сравнивает std::string с LinkedList, Вы хотите сравнить word в temp со строкой.

Измените функцию на:

bool LinkedList::search(const std::string& searchItem) const

Большинство других ваших ошибок являются вариациями на эту тему.

Редактировать: ошибка LinkedList за linkedList — пожалуйста, не указывайте структуру и класс с тем же именем, но в разных случаях. Это очень запутанно.

1

Другие решения


По вопросам рекламы [email protected]