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

时间:2014-09-24 00:48:22

标签: c++ visual-studio

我遇到了一些运营商的问题。我的搜索和deleteWord中的运算符。在搜索中,我试图检查列表中的单词是否与搜索词匹配。在deleteWord中,我试图检查我要删除的单词是否与列表中的单词完全匹配,然后将其删除。

我已经查找了如何修复我的操作员,但我不明白。提前谢谢。

有些错误:

错误1错误C2784:'bool std :: opeator> =(const std :: basic_string< _Elem,_Traits,_Alloc>&,const_Elem *):无法为'const'推断'const_Elem *'的模板参数链表'

错误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智能感知:没有操作符“==”匹配这些操作数

#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

1 个答案:

答案 0 :(得分:1)

这一行:

if (temp->word >= searchItem)

std::stringLinkedList进行比较。您想要将word中的temp与字符串进行比较。

将功能更改为:

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

您的其他大多数错误都是此主题的变体。

编辑:误导LinkedList linkedList - 请不要使用相同名称但不同情况的结构和类。这非常令人困惑。