我想使用模板来实现通用链表,其中链表的每个节点都包含一个类 T 的元素。
这是linked_list.h
#ifndef LIN_LIS
#define LIN_LIS
#include "node.h"
template <class T>
class LinkedList {
public:
//
Node<T>* head;
//
LinkedList() : head( 0 ) {}
//
~LinkedList();
};
#endif
这是linked_list.cpp
#include <iostream>
#include "linked_list.h"
using std::cout;
using std::endl;
template <class T>
LinkedList<T>::~LinkedList() {
Node<T>* p = head;
Node<T>* q;
while( p != 0 ) {
q = p;
//
p = p->next;
//
delete q;
}
}
这是测试文件
#include <iostream>
#include <ctime>
#include "linked_list.h"
using std::cout;
using std::endl;
int main() {
//
LinkedList<int> l;
return 0;
}
这是我的makefile
test_linked_0: test_linked_0.o linked_list.o
$(CC) -o test_linked_0 test_linked_0.o linked_list.o $(CFLAGS)
我收到以下错误:
g++ -o test_linked_0 test_linked_0.o linked_list.o -I.
Undefined symbols for architecture x86_64:
"LinkedList<int>::~LinkedList()", referenced from:
_main in test_linked_0.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [test_linked_0] Error 1
任何帮助将非常感激。