2017-04-10 57 views
-1

我在鏈表的末尾插入一個字符串。當我編譯我的文件時,我得到2個錯誤:在鏈表的末尾插入字符串

錯誤:'setData'未在此範圍內聲明 setData(* string_p);

錯誤:'getNext'未在此範圍內聲明 newNode = getNext();

然而,它們是在我使用它們(在上面的方法中定義)之前定義的,所以我不明白這個錯誤。

#include <iostream> 
    #include <string> 

    using std::string; 
    using std::cout; 
    using std::endl; 

    #define SUCCESS 0 
    #define FAIL 1 


    // Represents an entry object in the linked-list 
    class ListEntry 
    { 
    public: 
    explicit  ListEntry(); 
    explicit  ListEntry(const char *string_p); 
      ~ListEntry(); 
    string  getData(); 
    void   setData(const char* string_p); 
    void   setData(string string); 
    ListEntry *getNext(); 
    ListEntry *getPrevious(); 
    ListEntry *prev_p; // pointer to previous entry in the linked-list 
    ListEntry *next_p; // pointer to next entry in the linked-list 

    private: 
    string   data;  // entry's string 
    }; 

    // Represents the linked-list object 
    class List 
    { 
    public: 
    List(); 
    ~List(); 

    bool printForward(); 
    bool printReverse(); 
    bool insert(const char *string_p); 

    private: 
    int  entryCount; // number of entries present in the linked-list 
    ListEntry *head_p;  // pointer to the first entry in the list 
    ListEntry *tail_p;  // pointer to the last entry in the list 
    }; 

    // ListEntry constructor 
    ListEntry::ListEntry() 
    { 
    this->prev_p = NULL; 
    this->next_p = NULL; 
    return; 
    } 

    // ListEntry constructor 
    ListEntry::ListEntry(const char *string_p) 
    { 
    this->data = string_p; 
    this->prev_p = NULL; 
    this->next_p = NULL; 
    return; 
    } 

    // List entry destructor 
    ListEntry::~ListEntry() 
    { 
    return; 
    } 

    // Return the stored string object 
    string ListEntry::getData() 
    { 
    return this->data; 
    } 

    // Set the internal string data from a char* 
    void ListEntry::setData(const char* string_p) 
    { 
    this->data = string_p; 
    } 

    // Set the internal string data from a string 
    void ListEntry::setData(string string) 
    { 
    this->data = string; 
    } 

    // Returns reference to the next entry in the list 
    ListEntry *ListEntry::getNext() 
    { 
    return this->next_p; 
    } 

    // Returns reference to the previous entry in the list 
    ListEntry *ListEntry::getPrevious() 
    { 
    return this->prev_p; 
    } 

我的插入函數(低於上述方法在我的程序):

bool List::insert(const char *string_p) 
    { 
     // Please write the list insert function 

     //new node to be inserted 
     ListEntry* newNode = new ListEntry(); 
     //List *newList = new List(); 

     if(newNode == NULL) 
     { 
      cout << "FAILED"; 
     } 
     else 
     { 
      setData(*string_p); //////ERROR HERE 
      if(this->head_p = NULL) 
      { 
      newNode = getNext(); //////ERROR HERE 
      newNode = this->head_p; 
      this->head_p = newNode; // newNode now points to the head node 
      this->entryCount++; 
      return SUCCESS; 
      } 
      else 
      { 
      ListEntry* temp = this->head_p; 
      while(temp -> next_p != NULL) 
      { 
       temp = temp -> next_p; 
      } 
      temp -> next_p = newNode; 
      this->entryCount++; 
      return SUCCESS; 
      } 

     } 
    } 
+1

另外,setData(* string_p);應該是setData(string_p); –

+1

'setData'和'getNext'屬於'ListEntry',但您在屬於'List'的方法中使用'this'。你的意思是'newNode-> getNext();'和'newNode-> setData(string_p);'? –

回答

1

您已經定義的功能,但你不使用他們,你有定義方式:

setData(*string_p); // Takes a const char*, but you have provided a char. 
        // *string_p dereferences the string pointer, giving the 
        // first char. 
newNode = getNext(); // getNext is a ListEntry function, but you are trying 
        // to use it in the context of List. This is also true of the 
        // above function. 
+0

但setData()需要'const char * string_p'並且我傳遞'const char * string_p'。有相同的類型嗎? – guy

+0

@guy不,你正在傳遞'* string_p',它是'const char *'解除引用。即字符。 –

+0

因此,如果我不解除引用即擺脫*,它們應該是相同的類型? – guy

0

函數setDatagetNext是類ListEntry的非靜態成員函數。所以他們必須使用成員訪問表達式來調用。

此外,這種呼叫

setData(*string_p); 

的提供的參數有不同的類型比的函數的期望。

你必須寫至少像

newNode->setFata(string_p); 

newNode->getNext(); 

但即使函數的調用將但從沒有意義的語法點正確此代碼段

 if(this->head_p = NULL) 
     { 
     newNode = newNode->getNext(); 
     newNode = this->head_p; 

因爲至少有內存泄漏。

而且這個if語句

if(newNode == NULL) 

,如果你使用new運算符的下面的調用纔有意義

ListEntry* newNode = new (std::nothrow) ListEntry(); 

功能可以看看下面的方式

bool List::insert(const char *string_p) 
{ 
    //new node to be inserted 
    ListEntry *newNode = new (std::nothrow) ListEntry(string_p); 

    bool success = newNode != nullptr; 

    if (success) 
    { 
     if (tail_p) 
     { 
      tail_p->next_p = newNode; 
      newNode->prev_p = tail_p; 
     } 
     else 
     { 
      head_p = newNode; 
     } 

     tail_p = newNode; 
     entryCount++; 
    } 

    return success; 
} 
+0

有這樣的想法,如果頭指針爲空,則使newNode成爲新的頭指針。我的代碼現在編譯,但我有seg故障。有什麼想法嗎? – guy

+0

@guy由於我寫這段代碼沒有意義。 –

+0

任何建議,這種泄漏是什麼? – guy

0

insert()方法執行全部錯誤。它應該看起來更像這樣:

int List::insert(const char *string_p) 
{ 
    //new node to be inserted 
    ListEntry* newNode = new ListEntry(string_p); 

    if (newNode == NULL) 
    { 
     cout << "FAILED"; 
     return FAIL; 
    } 

    if (this->head_p == NULL) { 
     this->head_p = newNode; 
    } 

    if (this->tail_p != NULL) 
    { 
     this->tail_p->next_p = newNode; 
     newNode->prev_p = this->tail_p; 
    } 
    this->tail_p = newNode; 

    this->entryCount++; 
    return SUCCESS; 
}