2014-09-24 53 views
-2

我遇到了一些操作員的問題。我的搜索和deleteWord中的運算符。在搜索中,我試圖檢查列表中的單詞是否與搜索詞匹配。在deleteWord中,我試圖檢查要刪除的單詞是否與列表中的單詞完全匹配,然後將其刪除。Visual Studio中的操作員錯誤(錯誤C2784)

我擡頭如何解決我的操作員,但我不明白。先謝謝你。

一些錯誤:

錯誤1個錯誤C2784:「布爾的std :: opeator> =(常量的std :: basic_string的< _Elem,_Traits,_Alloc> &,const_Elem *):不能推導出模板參數用於 'const的鏈表'

錯誤 'const_Elem *' 2錯誤C2784: '布爾的std ::操作> =(常量_Elem *,常量性病:: basic_string的< _Elem,_Traits,_Alloc> &)':不能從'std :: string'推導'const _Elem *'的模板參數

錯誤3錯誤C2784:'bool std :: operator> =(const std :: basic_string < _Elem,_Traits,_Alloc> &,const std :: basic_string < _Elem,_Traits,_Alloc> &)':無法推導出模板參數關於 '常量性病:: basic_string的< _Elem,_Traits,_Alloc> &' 從 'const的鏈表'

錯誤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

您收到的確切錯誤或錯誤是什麼? – 2014-09-24 00:56:38

+0

你確定需要返回'const LinkedList&'作爲'operator ='的返回嗎?爲什麼'const'?最後你錯過了'#endif'。 – vsoftco 2014-09-24 01:03:43

+0

@RetiredNinja我添加了一些錯誤,我有。 – Miz 2014-09-24 01:07:44

回答

1

這條線:

if (temp->word >= searchItem) 

LinkedList進行比較的std::string。您想要將temp中的word與字符串進行比較。

更改功能:

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

關於這一主題的其他大部分的錯誤都是變化。

編輯:mistook LinkedList對於linkedList - 請不要具有相同名稱但不同情況的結構和類。這很混亂。

+0

功能的變化奏效。謝謝。我承諾永遠不要讓我的結構和類在不同的情況下命名相同(^^^ – Miz 2014-09-24 01:19:57

+0

在'operator ='的返回類型中擺脫'const','a = b = c'可以工作,但想一想如果你做了'(a = b)= c'會發生什麼。 – vsoftco 2014-09-24 01:34:41