# 我写了个链表,不知道哪里错了,编译通不过,求助

0

//=========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:
int Count;

};

#endif // CLIST_H```

//===============Clist.cpp=============//

```#include "Clist.h"
#include <cassert>
#include <iostream>
using namespace std;

template <typename T>
{
}

template <typename T>
Clist<T>::~Clist()
{
{
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;
Count += 1;
}
else if (i == 1)   //我插的这个结点将会是List的第一个结点(头结点) .
{
Node<T> *p = new Node<T>;
p->data = data;
Count += 1;
}
else
{
Node<T> *p = new Node<T>;
p->data = data;
Node<T> *tmp = new Node<T>;
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;
delete p;
Count -= 1;
}
else                                    //删除的是除首元素之外的其它元素
{
Node<T> *p;
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()
{
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))```
ii沙漠 | 初学一级 | 园豆：114

0

Launcher | 高人七级 |园豆：45045 | 2014-11-27 09:17

ii沙漠 | 园豆：114 (初学一级) | 2014-11-27 12:26

0

Mr.Brian | 园豆：1518 (小虾三级) | 2014-11-27 09:24

您需要登录以后才能回答，未注册用户请先注册