2017-01-18 16 views
1

ユーザー情報を取得してノードに格納するLinkedListを作成しようとしています。その後、ユーザー情報が表示されます。私はすでにこれをトラフを働いてきたし、これは私が持っているものです。C++シンプルなLinkedListが範囲外の変数

#include <iostream> 
    using namespace std; 

    template <typename T> 
    class Node 
    { 
    T data; 
    Node* next; 
}; 

template <typename T> 
class LinkedList 

{ 
    public: 
    //Default constructor 
    LinkedList(){ 
     head = NULL; 
     tail = NULL; 
    } 

    void addData(T val){ 
     Node* newNode = new Node; 
     newNode->data = val; 
     newNode->next = NULL; // we add newNode at end of linked list 
     if(head == NULL){ 
      head = newNode; 
      tail = newNode; 
     } 

     else{ 
      tail ->next = newNode; 
      tail = newNode; 
     } 
    } 

    void printData() { 
     for(Node* i = head; i!=NULL; i = i -> next){ 
      cout << i->data << " " ; 

     } 
    } 

    private: 
    //Declare the head and tail of the LinkedList 
     Node* head; 
     Node* tail; 

}; 

int main(){ 

    LinkedList<string> usrInput; 
    cout << "Enter a number or word : "; 
    string input ; 
    cin >> input; 

    usrInput.addData(input); 
    usrInput.printData(); 



    LinkedList<int> numbers; 
    while (true) 
    { 
     cout<< "Enter a number (-1 to stop): "; 
     int num; 
     cin >> num; 

     if (num == -1) 
      break; 

     numbers.addData(num); 
    } 
    numbers.printData(); 


} 

問題は、私は私のプログラムをコンパイルするとき、私はスコープのメンバ変数のうちに言及エラーの束を得ることです。メンバー変数はプライベートではないと思われますか?


これは、私は、デバッガのために持っているものです。

46:9: error: invalid use of template-name 'Node' without an argument list 

47:9: error: invalid use of template-name 'Node' without an argument list 

In constructor 'LinkedList<T>::LinkedList()': 

18:9: error: 'head' was not declared in this scope 

19:9: error: 'tail' was not declared in this scope 

In member function 'void LinkedList<T>::addData(T)': 

23:13: error: missing template arguments before '*' token 

23:15: error: 'newNode' was not declared in this scope 

23:29: error: invalid use of template-name 'Node' without an argument list 

26:13: error: 'head' was not declared in this scope 

28:13: error: 'tail' was not declared in this scope 

32:13: error: 'tail' was not declared in this scope 

In member function 'void LinkedList<T>::printData()': 

38:17: error: missing template arguments before '*' token 

38:19: error: 'i' was not declared in this scope 

38:23: error: 'head' was not declared in this scope 
+0

'ノード *ヘッド;' – greatwolf

答えて

3

クラスのデータメンバーは、デフォルトではプライベートです。それらは構造体にデフォルトで公開されています。つまり、次のようになります。

class Node 
    { 
    T data; 
    Node* next; 
}; 

LinkedListクラスには、ノードのメンバーが表示されません。試してみてくださいいずれかを使用して:

struct Node { 
    T data; 
    Node* next; 
}; 

または

class Node { 
    public: 
     T data; 
     Node* next; 
}; 

文体、ほとんどの実際の実装の巣のLinkedListクラス内部のプライベートメンバ構造体としてノード。

私は私の答えは:)私は後半笑だ場合でも、あまりにも他の人のためのhelpullことを願っています
+0

ありがとう!ネイティブのC++開発者ではありません。同じケーキだけ異なる風味。これは私の問題を解決しました。 –

+0

N.P.あなたの質問を将来の人々のために答えられるようにすることを検討してください。 – tegtmeye

+0

親愛なるtegtmeye様、私はどのように質問に回答しますか?私はここで新しいです:D。私はasyncTaskを扱う他の1つの質問をマークしなければならないので、私に知らせてください。 –

1

Nodeだけで、あなたのコード内の有効な型を指すものではありません。 Node<T>を使用する必要があります。

コンパイラがNodeに遭遇すると、それは不正な使用としてフラグが立てられ、変数またはメンバを使用したときに後でコンパイルでエラーが発生し、その型で定義されている変数またはメンバが破棄されます。

Node<T>で完全な型を使用するように切り替えると、Node<T>クラスのメンバーの私的なメンバーシップを持つ他の答えに記載されている問題が発生します。

+0

ありがとう、私はラインの型の不一致引数を解決しました46と47は既にあります。 –

+0

私の質問は今私の変数の範囲です。なぜ彼らは範囲外ですか? –

+0

@ErickRamirezノードのすべての用途をノードに置き換えましたか? –

0

#include <iostream> 
    using namespace std; 

template <typename Type> 
class Node 
{  
    template<typename T> friend class LinkedList;/* you need to make your linkedList 
     class a friend variable to be able to,acces the private members directly from the objects inside the class Linkedlist. 
     other option its to make them public, but it is not recomended and other option si to make functions to acces de data, but this will be very messy, just make your linkedlist class friend*/ 
    Type data; 
    Node<Type>* next; // you need to specify which data type the node will be 
}; 

template <typename T> 
class LinkedList 
{ 
public: 
//Default constructor 
LinkedList(){ 
    head = NULL; 
    tail = NULL; 
} 

void addData(T val){ 
    // you can't forget to specify the data type since it is a template!! 
    Node<T>* newNode = new Node<T>(); 
    newNode->data = val; 
    newNode->next = NULL; // we add newNode at end of linked list 
    if(head == NULL){ 
     head = newNode; 
     tail = newNode; 
    } 

    else{ 
     tail ->next = newNode; 
     tail = newNode; 
    } 
} 

void printData() { 
    // again specify the data type of the template 
    for(Node<T>* i = head; i !=NULL; i = i -> next){ 
     cout << i->data << "\n" ; 

    } 
} 

private: 
    //Declare the head and tail of the LinkedList 
    Node<T>* head;// Again specify the data type of the template 
    Node<T>* tail;// here the same thing 

}; 

int main(){ 

    LinkedList<string> usrInput; 
    cout << "Enter a number or word : "; 
    string input ; 
    getline(cin,input);// use the get line function to get strings from standar input 

    usrInput.addData(input); 
    usrInput.printData(); 

    // Declare the num variable otuside and initialize it, you should always initialize the variables 
    int num(0); 
    LinkedList<int> numbers; 
    while (true) 
    { 
     cout<< "Enter a number (-1 to stop): "; 
     // you need to validate the input if you don't want to run an infinite loop 
     if(!(cin >> num)){ 
      cin.clear(); 
      cin.ignore(100,'\n'); 
     } 
     else { 
     // if we succes with the imput you can add the data to the linked list :) 
      if (num == -1){break;} 
      numbers.addData(num); 
     } 
    } 
     numbers.printData(); 


    } 
関連する問題