2017-06-15 8 views
0

"farming"という文字を含むテキストファイルをノードのリンクリストに読み込もうとしています。私はノードの構造を持つNumberListという名前のクラスを作った。ここにヘッダーがあります。ファイルをリンクリストに読み込むクラスのノード構造を持つC++

#ifndef NUMBERLIST 
#define NUMBERLIST 
#include <iostream> 

using namespace std; 

class NumberList 
{ 
protected: 
//declare a class for the list node 
//constructor to initialize nodes of list 
struct ListNode 
{ 
    char value; 
    ListNode *next; 

    // Constructor 
    ListNode(char value1, ListNode *next1 = NULL) 
    { 
     value = value1; 
     next = next1; 
    } 
}; 

ListNode *head; //pointer to head of the list 

public: 
NumberList() { head = NULL; } //constructor 
~NumberList();  //destructor 
void displayList() const; //print out list 
void reverse(); 

}; 
#endif 

問題が発生している箇所では、テキストファイルをmain()のリンクリストに読み込もうとしています。

#include "Numberlist.h" 
#include "ReliableNumberList.h" 
#include <iostream> 
#include <fstream> 


using namespace std; 

int main() 
{ 

ListNode *letterList = nullptr; //create a linked list 
char letter; 
        //This is where I read the file into the list 
//open the file 
ifstream letterFile("linkedText.txt"); 
if (!letterFile) 
{ 
    cout << "Error in opening the file of letters."; 
    exit(1); 
} 
//read the file into a linked list 
while (letterFile >> letter) 
{ 
    //create a node to hold this letter 
    letterList = new ListNode(letter, letterList); 
    //missing a move to the next node? 
} 
return 0; 
} 

この読み取りファイルのサンプルは私の教科書から来たが、構造別のクラスに位置していなかったにその読み:

は、ここで私がメインに持っているものです。私の人生では、ListListクラスのListNode構造体をどのように参照するのか分かりません。 Visual Studioでは、ListNodeとletterListが定義されていないことを示しています。私はNumberListクラスからそれらを適切に参照していないので、そのことを知っています。

ご協力いただければ幸いです。

あなたの問題への迅速な解決策は、この可能性が
+1

ようこそスタックオーバーフロー。 [The Tour](http://stackoverflow.com/tour)を読み、[ヘルプセンター](http://stackoverflow.com/help/asking)の資料を参考にしてください。ここに聞いてください。 –

+0

'ListNode'構造体は外界から'保護されているようです。おそらく、 'NumberList'がchar(内部/保護された)リストに' char'を追加するメソッド( 'push_back(...)')を持つような考え方でした。このように 'NumberList'オブジェクトはノードの作成と破壊を管理し、' main() 'では毎回直接' ListNode'を作成するのではなく、かなり 'list.push_back(letter)'だけ書く必要があります。 – Drop

+0

さらに、 'ListNode'は実際には' NumberList :: ListNode'です。 'ListNode'は' NumberList'の中にあります。 'NumberList'は' NumberList'であるため、完全に修飾する必要はありません。 – user4581301

答えて

0

//------------------------------NumberList.hpp----------------------------- 

#ifndef NUMBERLIST_HPP 
#define NUMBERLIST_HPP 
#include <iostream> 

class NumberList{ 
protected: 
    //Protected Members can't be used outside the class 
    struct ListNode{ 
     char value; 
     ListNode *next; 
     ListNode(char value1, ListNode *next1 = NULL){ 
      value = value1; 
      next = next1; 
     } 
    }; 
    ListNode *head, *tail; //class members 
    //head always points at the 1st letter, tail is used for quick adding at the end 
public: 
    NumberList() { head = NULL; tail = NULL; } 
    ~NumberList(); //don't forget to deallocate space properly at the end 
    void displayList() const; //print out list 
    void reverse(); 
    void add(char newchar) { 
     //allocate a new node using the ListNode constructor, by default, next1 will be null 
     ListNode *newNode = new ListNode(newchar); //equvalent to ListNode(newchar, NULL); 
     if (tail == NULL) { //if no elements in the list, both show to newNode 
      tail = newNode; 
      head = newNode; 
     }else{ 
      tail->next = newNode; //make last node -> next pointer, point to newNode (new last node) 
      tail = tail->next; //make current last node be the actual last node 
     } 
    } 
}; 
#endif 

//------------------------------Main.cpp----------------------------- 
#include "Numberlist.hpp" 
#include <iostream> 
#include <fstream> 

using namespace std; 

int main(){ 
    ifstream letterFile("linkedText.txt"); 
    if (!letterFile){ 
     cout << "Error in opening the file of letters."; 
     exit(-1); 
    } 

    NumberList numberList; 

    char letter; 
    while (letterFile >> letter) numberList.add(letter); 
} 

それは少しあなたは、もはやあなたのリストにリストノードを追加しないように、あなたのロジックを変更 が、私はあなたがしたくないと思われます、とにかくその代わりに、文字「 」をリストに直接追加し、そのノードがノードを処理するようにしてください(ノード構造が保護されているので意味があります)。

確かにクラスはより洗練されていなければなりませんが、これは最初の問題を解決するはずです。

+0

ありがとう!これは本当に正しい方向に私を指摘した。上記のようにクラスの追加メソッドを作成しました。ちょっとした作業で、私は割り当てのためにすべての作業を行うことができました。 – Inteligirl

関連する問題