ユーザー情報を取得してノードに格納する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
'ノード *ヘッド;' –
greatwolf