Little Prince~ 2017-10-30 08:59 采纳率: 0%
浏览 1495

C++ 用于实现链表的类模板、继承、类模板声明问题

应该是语法问题,因为如果不选择使用继承的话编译就完全没问题。对于如何声明继承的类模板,定义继承的类模板以及在主类中声明继承类模板感觉好模糊。
后面一大串功能的实现应该和错误没有关系,可以选择忽略,主要是在前三个类。报错的点有问题。
我使用的是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;

}

  • 写回答

1条回答 默认 最新

  • 路ren甲 2017-10-30 12:17
    关注

    链表呀!思考一下*+*

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题