我编写了一个从list母类继承的链表linkList子类并把声明写在linkList.h文件中,实现写在linkList.cpp中,
但是我发现只用#include "linkList.h"的话会报错如下
但是如果加上#include "linkList.cpp" 则不会报错。
希望大神告知错误所在,以及教我一下如何正确的书写头文件和源文件!
以下是代码:
linkList.h:
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
class OutOfBound {};
class IllegalSize {};
template <class T>
class list
{
public:
virtual int length() const = 0;
virtual void clear() = 0;
virtual void insert(int i,const T& x) = 0;
virtual void remove(int i) = 0;
virtual T visit(int i) const = 0;
virtual int search(const T& x) const = 0;
virtual void traverse() const = 0;
virtual ~list() {};
};
template <class T>
class linkList:public list<T>
{
private:
struct node
{
T data;
node *prev, *next;
node(const T &x, node* p = NULL, node* n = NULL)
{
data = x;
prev = p;
next = n;
}
node():next(NULL), prev(NULL) {}
~node() {}
};
node *head, *tail;
int currentLength;
node* move(int i) const;
public:
linkList();
~linkList()
{
clear();
delete head;
delete tail;
}
int length() const
{
return currentLength;
}
void clear();
void insert(int i,const T& x);
void remove(int i);
T visit(int i) const;
int search(const T& x) const;
void traverse() const;
};
#endif // LINKLIST_H_INCLUDED
linkList.cpp:
//file:
#include <iostream>
#include <cstdio>
#include "linkList.h"
using namespace std;
template <class T>
typename linkList<T>::node* linkList<T>::move(int i) const
{
node* p = head -> next;
if(i < 0 || i > currentLength) throw OutOfBound();
while(i > 0)
{
p = p -> next;
i--;
}
return p;
}
template <class T>
linkList<T>::linkList()
{
head = new node;
tail = new node;
head -> next = tail;
tail -> prev = head;
currentLength = 0;
}
template <class T>
void linkList<T>::clear()
{
node *p, *q;
p = head -> next;
while(p != tail)
{
q = p -> next;
delete p;
p = q;
}
head -> next = tail;
tail -> prev = head;
currentLength = 0;
}
template <class T>
void linkList<T>::insert(int i, const T& x)
{
node *pos = move(i);
node *tmp = new node(x, pos -> prev, pos);
pos -> prev -> next = tmp;
pos -> prev = tmp;
++currentLength;
}
template <class T>
void linkList<T>::remove(int i)
{
node *pos = move(i);
pos -> prev -> next = pos -> next;
pos -> next -> prev = pos -> prev;
delete pos;
--currentLength;
}
template <class T>
int linkList<T>::search(const T& x) const
{
int i = 0;
node* p = head -> next;
while(p != tail && p -> data != x)
{
p = p -> next;
i++;
}
if( p == tail)
return -1;
else
return i;
}
template <class T>
T linkList<T>::visit(int i) const
{
node* p = move(i);
return p -> data;
}
template <class T>
void linkList<T>::traverse() const
{
node *p = head -> next;
while(p != tail)
{
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
}
main.cpp:
#include <iostream>
#include "linkList.h"
using namespace std;
int main()
{
linkList<int> l1;
int i;
char ch;
for(i = 0; i < 100; i++)
{
l1.insert(i, i);
}
l1.traverse();
ch = cin.get();
for(i = 50; i > 0; i--)
{
l1.remove(i);
}
l1.traverse();
ch = cin.get();
cout << l1.length() << endl;
ch = cin.get();
for(i = 0; i < l1.length() ; ++i)
{
cout << l1.visit(i) << endl;
}
l1.traverse();
ch = cin.get();
for(i = 60; i < 80 ; ++i)
{
cout << l1.search(i) << endl;
}
l1.traverse();
ch = cin.get();
return 0;
}