Visual Studio中的操作员错误(错误C2784)

Operator Error in Visual Studio (error C2784)

本文关键字:错误 C2784 操作员 Studio Visual      更新时间:2023-10-16

我的一些运算符有问题。我搜索和删除Word中的运算符。在搜索中,我试图检查列表中的单词是否与搜索单词匹配。在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::string"推导出"const_Elem*"的模板参数

错误3错误C2784:"bool std::operator>=(const std::basic_string<_Elem,_Traits,_Alloc>&,const std::basic_strong&lt_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)

正在将CCD_ 1与CCD_。您要将temp中的word与字符串进行比较。

将功能更改为:

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

你的大多数其他错误都是这个主题的变体。

编辑:将LinkedList误认为linkedList-请不要有名称相同但大小写不同的结构和类。这很令人困惑。