2017-04-10 13 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いいえ、あなたは 'const char *'逆参照された '* string_p'を渡しています。すなわち、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) 

新しい演算子の次の呼び出しを使用する場合は意味をなさないだろうことは

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

headポインタがnullの場合、newNodeを新しいHeadポインタにします。私のコードは今コンパイルされていますが、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; 
} 
関連する問題