由C++的继承引出来的无法通过编译的问题

wallace-lll 2015-05-21 04:12:18
对于我这种对C++特性还不熟悉的新手来说,设计类简直有点找虐。代码如下,基类list里面全都是纯虚函数,派生类seqList里公有继承了list类,但无法通过编译。
这个是基类list的情况:

template <typename elemType>
class list
{
public :
virtual void clear() = 0;
virtual int lenght()const = 0;
virtual void insert(int i, const elemType &x) = 0;
virtual void remove(int i) = 0;
virtual int search(const elemType &x)const = 0;
virtual elemType visit(int i)const = 0;
virtual ~list(){}
};

这个是基类seqList的情况:
函数具体的实现过程没有写出,因为我觉得问题不在这里

template <typename elemType>
class seqList : public list<elemType>
{
private :
elemType *data;
int currentLength;
int maxSize;
void doubleSpace();

public :

seqList(int initSize = 10);
~seqList() {delete [] data;}
void clear() {currentLength = 0;}
int length()const {return currentLength;}
void insert(int i, const elemType &x);
void remove(int i);
int search(const elemType &x)const;
elemType visit(int i)const {return data[i];}
void traverse()const;
};

template <typename elemType>
void seqList<elemType> :: doubleSpace() //扩容
{

}

template <typename elemType>
seqList<elemType> :: seqList(int initSize) //顺序表初始化
{

}

template <typename elemType> //在第i个位置插入元素
void seqList<elemType> :: insert(int i, const elemType &x)
{

}

template <typename elemType>
void seqList<elemType> :: remove(int i)
{

}

template <typename elemType>
int seqList<elemType> :: search(const elemType &x)const
{

}

template <typename elemType>
void seqList<elemType> :: traverse()const
{

}

接下来是对该类进行简单的测试了

void testForSeqList()
{
seqList<int> l;
cout << "currentLength : " << l.currentLength << endl;
cout << "maxSize : " << l.maxSize << endl;
l.insert(1, 1);
l.insert(1, 2);
l.insert(1, 3);
cout << "currentLength : " << l.currentLength << endl;
cout << "maxSize : " << l.maxSize << endl;
l.traverse();
l.remove(1);
l.traverse();

}

接下来编译器就不干了,报错。这些currentLength, maxSize是私有的,无法访问这就算了。让我无法理解的是:
编译器指着testForSeqList()函数中的seqList<int> l这句说:
cannot declare variable 'l' to be of abstract type 'seqList<int>'
becasue the following virtual function are pure within 'seqList<int>'
这是怎么回事了,请懂的同学给我讲讲!谢谢!
...全文
412 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wallace-lll 2015-05-21
  • 打赏
  • 举报
回复
原来如此,好粗心啊
www_adintr_com 2015-05-21
  • 打赏
  • 举报
回复
基类里面的 length 拼写错了,你写的是:

virtual int lenght()const = 0;
而派生类里面的是

int length()const {return currentLength;}
这样有一个函数还是纯虚的,就不能实例化了

64,647

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧