これは、テンプレート リスト コードの縮小バージョンです ( http://www.daniweb.com/software-development/cpp/threads/237391/c-template-linked-list-helpから適合) 。
リストは「ノードは型ではありません」と文句を言います (コンパイル エラー)。これはなぜですか。修正方法は何ですか?
「class Node」を「struct Node」(および関連する変更) に置き換えてみたところ、構造体バージョンは正常に機能しました。したがって、主な問題は次のように思われます: テンプレート クラスは別のテンプレート クラスにどのようにアクセスするのでしょうか?
#include <iostream>
using namespace std;
template <typename T>
class Node
{
public:
Node(){}
Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
Node<T>* getLink( ) const { return link; }
const T getData( ) const { return data; }
void setData(const T& theData) { data = theData; }
void setLink(Node<T>* pointer) { link = pointer; }
private:
T data;
Node<T> *link;
};
template <typename T>
class List {
public:
List() {
first = NULL;
last = NULL;
count = 0;
}
void insertFirst(const T& newData) {
first = new Node(newData, first);
++count;
}
void printList() {
Node<T> *tempt;
tempt = first;
while(tempt != NULL){
cout << tempt->getData() << " ";
tempt = tempt->getLink();
}
}
~List() { }
private:
Node<T> *first;
Node<T> *last;
int count;
};
int main() {
List<int> myIntList;
cout << "Inserting 1 in the list...\n";
myIntList.insertFirst(1);
myIntList.printList();
cout << endl;
List<double> myDoubleList;
cout << "Inserting 1.5 in the list...\n";
myDoubleList.insertFirst(1.5);
myDoubleList.printList();
cout << endl;
}