由C++的继承引出来的无法通过编译的问题
这个是基类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>'
这是怎么回事了,请懂的同学给我讲讲!谢谢!
相关推荐
关于C++中菱形继承和虚继承的问题总结
前言 菱形继承是多重继承中跑不掉的,Java拿掉了多重继承,辅之以接口。C++中虽然没有明确说明接口这种东西,但是只有纯虚函数的类可以看作Java中的接口。在多重继承中建议使用“接口”,来避免多重继承中可能出现的各种问题。本文将给大家详细介绍关于C++菱形继承和虚继承的相关内容,分享出来供大家参考学习,话不多说了,来一起看看详细的介绍吧。 继承: 1. 单继承–一个子类只有一个直接父类时称这个继承关系为单继承 2. 多继承–一个子类有两个或以上直接父类时称这个继承关系为多继承 例如下面这两个例子: 例一(单继承): class A { public: int _a;
C++中多重继承导致的编译错误:排查与解决指南
C++中的多重继承虽然强大,但也带来了许多挑战,其中编译错误是最常见的问题之一。通过了解编译错误的产生原因、掌握排查方法和解决方案,我们可以有效地避免和解决这些问题。在实际开发中,要谨慎使用多重继承,遵循良好的设计原则,确保代码的质量和可维护性。希望本文能够帮助你更好地理解和处理C++中多重继承导致的编译错误。
CPP.rar_多态 .cpp_继承与多态_继承与多态C++
本代码中有:“包含与继承”、“虚函数与多态”、“模板”程序例子
cpp代码-测试C++默认继承方式
cpp代码-测试C++默认继承方式
CPP和C文件混合编译问题
在只有C++库而源码是C的情况下,使用封装层的技巧就是封装层使是用g++编译,但是只在封装头文件函数声明中加了extern “C”,而调用的C++静态库函数的#include未加,因此调用的函数还是按C++的规则解析,制作库时可以实现连接。通过2.2我们可以知道在C++的头文件中只要把函数的声明放在extern “C”{}的大括号范围之内,就可以让g++在编译这个函数时生成中间符号名时按照C的规则而不是按照C++的规则,所以这样的函数就可以和C的库进行共同链接。在C中是未进行定义的,可以用来检测编译环境。


