应该是语法问题,因为如果不选择使用继承的话编译就完全没问题。对于如何声明继承的类模板,定义继承的类模板以及在主类中声明继承类模板感觉好模糊。
后面一大串功能的实现应该和错误没有关系,可以选择忽略,主要是在前三个类。报错的点有问题。
我使用的是VS 2017。
真的查遍了百度翻遍了书实在是求助无门,谢谢好心人能为我指出问题所在。
谢谢!
#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
/*代码在继承模板类、类声明语法、类定义语法、List中的嵌套类声明语法中有错。
下面的调试会报错。但如果取消使用继承,程序能够正常编译通过。
*此程序用于实现List,算法与数据结构C++书中的例子。
*List类中的成员函数定义未全部完成,止于此。/
template class List;
template class Node;
template class const_iterator;
template class iterator : public const_iterator;
template
class List
{
private:
Node *head, *tail;
class const_iterator;
class iterator : public const_iterator;
int theSize;
void init();
public:
List() { init(); }
List(const List &rhs);
~List();
int size() const { return theSize; }
bool empty() const { return size() == 0; }
void clear()
{
while (!empty())
pop_front();
}
void erase(iterator iter);
iterator begin() { return iterator(head->next;) }
iterator end() { return iterator(tail->prev); }
const_iterator begin() const { return const_iterator(head->next); }
const_iterator end() const { return const_iterator(tail->prev); }
void pop_front();
void pop_back();
void push_front(const Object &x);
void push_back(const Object &x);
Object &front();
const Object &front() const;
Object &back();
const Object &back() const;
};
template
class List::const_iterator
{
public:
const_iterator() : current(NULL) { }
const Object &operator* () { return retrieve(); }
const_iterator &operator++()
{
current = current->next;
return *this;
}
const_iterator operator++(int)
{
const_iterator old = *this;
++(*this);
return old;
}
bool operator==(const Object &rhs) const { return current = rhs.current; }
bool operator!=(const Object &rhs) const { return !(current = rhs.current); }
protected:
Node *current;
Object &retrieve() { return current->data; }
const_iterator(Node<Object> *n) : current(n) { }
friend class List<Object>;
};
template
class List::iterator : public const_iterator
{
public:
iterator() { }
Object &operator*() { return retrieve(); }
iterator operator++()
{
current = current->next;
return *this;
}
iterator operator++(int)
{
iterator old = *this;
++(*this);
return old;
}
bool operator==(const Object rhs) { return current == rhs.current; }
bool operator!=(const Object rhs) { return !(current == rhs.current) }
protected:
iterator(Node *n) : current(n) { }
friend class List;
};
template
class Node
{
public:
Node *prev, *next;
Object data;
Node(const Object &d = Object(), Object *p = NULL, Object *n = NULL) : data(d), prev(p), next(x)
{ }
};
template
void List::push_front(const Object &x)
{
Node *n = new Node;
n->data = x;
head->next->prev = n;
n->next = head->next;
n->prev = head;
head->next = n;
theSize++;
}
template
void List::push_back(const Object &x)
{
Node *n = new Node;
n->data = x;
tail->prev->next = n;
n->prev = tail->prev;
n->next = tail;
tail->prev = n;
theSize++;
}
template
void List::erase(iterator iter)
{
Node *p = iter.current;
p->next->prev = p->prev;
p->prev->next = p->next;
theSize--;
delete p;
}
template
void List::pop_front()
{
if (head->next == tail)
cout << "List is empty" << endl;
else
erase(begin());
}
template
void List::pop_back()
{
if (head->next == tail)
cout << "List is empty" << endl;
else
erase(end());
}
template
void List::init()
{
theSize = 0;
head = new Node;
tail = new Node;
tail->prev = head;
head->next = tail;
}
template
List::~List()
{
clear();
delete head;
delete tail;
}
int main( )
{
system("pause");
return 0;
}