//=========Clist.h==========//
#ifndef CLIST_H #define CLIST_H #define NULL 0 template <typename T> struct Node{ T data; Node* next; Node():data(T()), next(NULL){} }; template <typename T> class Clist { public: Clist(); virtual ~Clist(); public: void InsertAt(int i, T data); void DeleteAt(int i); bool Length() { return Count; } void Print(); protected: Node<T>* Head; int Count; }; #endif // CLIST_H
//===============Clist.cpp=============//
#include "Clist.h" #include <cassert> #include <iostream> using namespace std; template <typename T> Clist<T>::Clist():Head(NULL), Count(0) { } template <typename T> Clist<T>::~Clist() { if (Head != NULL) { Node<T> *p = Head; Head = Head->next; delete p; } } template <typename T> void Clist<T>::InsertAt(int i, T data) { assert(i >= 0 && i <= Count+1); if (i == 0) //表示链表刚刚创建还没有结点 , 那么我给它一个结点 . { assert(Count == 0); Node<T> *p = new Node<T>; p->data = data; p->next = NULL; Head = p; Count += 1; } else if (i == 1) //我插的这个结点将会是List的第一个结点(头结点) . { Node<T> *p = new Node<T>; p->data = data; p->next = Head; Head = p; Count += 1; } else { Node<T> *p = new Node<T>; p->data = data; Node<T> *tmp = new Node<T>; tmp = Head; for (int j = 1; j < i; j ++) tmp = tmp->next; p->next = tmp->next; tmp->next = p; Count += 1; } } template <typename T> void Clist<T>::DeleteAt(int i) { assert( i <= Count && i > 0); if (i == 1) //表示要删除链表的首元素 { Node<T> *p; p = Head; Head = Head->next; delete p; Count -= 1; } else //删除的是除首元素之外的其它元素 { Node<T> *p; Node<T> *q = Head; for (int j = 1; j < i; j ++) q = q->next; p = q->next; q = q->next->next; delete p; Count -= 1; } } template <typename T> void Clist<T>::Print() { Node<T> *p = Head; if (p != NULL) { cout << p->data << " "; p = p->next; } }
//==============main.cpp==============//
#include "Clist.h" #include <iostream> using namespace std; int main() { Clist<int> L; L.InsertAt(0, 2); L.InsertAt(1, 1); L.InsertAt(3, 4); L.InsertAt(3, 3); L.Print(); return 0; }
求大神帮帮忙啊. 编译好像也不报错啊,但是提示错误是这样的:
g++ -o bin/Debug/list obj/Debug/src/Clist.o obj/Debug/src/main.o obj/Debug/src/main.o:在函数‘main’中: /home/q/workspace/list/src/main.cpp:7:对‘Clist<int>::Clist()’未定义的引用 /home/q/workspace/list/src/main.cpp:8:对‘Clist<int>::InsertAt(int, int)’未定义的引用 /home/q/workspace/list/src/main.cpp:9:对‘Clist<int>::InsertAt(int, int)’未定义的引用 /home/q/workspace/list/src/main.cpp:10:对‘Clist<int>::InsertAt(int, int)’未定义的引用 /home/q/workspace/list/src/main.cpp:11:对‘Clist<int>::InsertAt(int, int)’未定义的引用 /home/q/workspace/list/src/main.cpp:12:对‘Clist<int>::Print()’未定义的引用 /home/q/workspace/list/src/main.cpp:13:对‘Clist<int>::~Clist()’未定义的引用 /home/q/workspace/list/src/main.cpp:13:对‘Clist<int>::~Clist()’未定义的引用 collect2: error: ld returned 1 exit status Process terminated with status 1 (0 minute(s), 0 second(s)) 0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
模板类不是这么用的。把 Clist 放置在单一的 .h 中去实现,或者在 .h 文件的最后 #include "Clist.cpp"。
谢谢Launcher大神.
代码没有仔细看,但是光看到对‘Clist<int>::Clist()’未定义的引用,异常信息。就可以看出,你肯定是没有定义,直接引用的。你最好把代码整理清楚,列表类的函数,都放到header里面,引用它就可以了啊。
谢谢Mr.Brian.