在数据结构中继承的问题
时间:2011-12-25
来源:互联网
C/C++ code
报错如下:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\main.o ..\src\main.cpp
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:13:15: error: cannot declare variable 'seqList' to be of abstract type 'SeqList<int>'
..\src\/SeqList.h:18:7: note: because the following virtual functions are pure within 'SeqList<int>':
..\src\/List.h:20:14: note: int List<T>::locaton(const T&) [with T = int]
..\src\/List.h:22:14: note: int List<T>::remove(int, const T&) [with T = int]
Build error occurred, build is stopped
Time consumed: 230 ms.
c++自学了两周,上课学的是c,但是数据结构用c++描述,现在对于c++不是狠明白,求教。
/* * List.h * * Created on: 2011-12-3 * Author: */ #ifndef LIST_H_ #define LIST_H_ template <typename T> class List { public: List(); virtual ~List(); virtual int getSize() = 0; virtual int getLength() = 0; virtual bool isEmpty() = 0; virtual bool isFull() = 0; virtual int locaton(const T& x) = 0; virtual int insert(int k, const T& x) = 0; virtual int remove(int k, const T& x) = 0; }; #endif /* LIST_H_ */ /* * SeqList.h * * Created on: 2011-12-3 * Author: */ #ifndef SEQLIST_H_ #define SEQLIST_H_ #include "List.h" #include <iostream> using namespace std; const int DEFAULTSIZE = 100; template <typename T> class SeqList : public List<T> { private: T* data; int size; int last; public: SeqList(int sz = DEFAULTSIZE); ~SeqList(); int getSize(); int getLength(); bool isEmpty(); bool isFull(); int location(const T& x); int remove(int k, T& x); int insert(int k, const T& x); void input(); void output(); }; template <class T> SeqList<T>::SeqList(int sz) { size = sz; data = new T[size]; last = -1; } template <typename T> SeqList<T>::~SeqList() { delete[] data; } template <typename T> int SeqList<T>::getLength() { return last; } template <typename T> int SeqList<T>::getSize() { return size; } template <typename T> bool SeqList<T>::isEmpty() { return last == -1; } template <typename T> bool SeqList<T>::isFull() { return last == size - 1; } template <typename T> int SeqList<T>::location(const T& x) { int i = 0; while (i <= last && data[i] != x) i++; if (i > last) { return -1; } else { return i; } } template <typename T> int SeqList<T>::remove(int k, T& x) { if (k > last || k < 0) { return 0; } last--; x = data[k]; for (int i = k; i <= last; i++) { data[i] = data[i + 1]; } return 1; } template <typename T> int SeqList<T>::insert(int k, const T& x) { if (k > last + 1 || k < 0 || last == size - 1) { return 0; } last++; int i; for (i = last; i > k; i--) { data[i] = data[i - 1]; } data[i] = x; return 1; } template <typename T> void SeqList<T>::input() { cout << "How many elements do you want to input?"; cin >> last; if (last > size - 1) { cout << "overflow!"; return; } for (int i = 0; i < last; i++) { cin >> data[i]; } } template <typename T> void SeqList<T>::output() { for (int i = 0; i < last; i++) { cout << data[i] << " "; } } #endif /* SEQLIST_H_ */ /* * main.cpp * * Created on: 2011-12-3 * Author: */ #include "SeqList.h" #include <iostream> using namespace std; int main() { SeqList<int> seqList(10); seqList.input(); cout << seqList.getLength() << endl; int k = 4; seqList.insert(3, k); seqList.output(); cout << endl; seqList.remove(3, k); seqList.output(); cout << endl; k = 9; cout << seqList.location(k) << endl; }
报错如下:
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o src\main.o ..\src\main.cpp
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:13:15: error: cannot declare variable 'seqList' to be of abstract type 'SeqList<int>'
..\src\/SeqList.h:18:7: note: because the following virtual functions are pure within 'SeqList<int>':
..\src\/List.h:20:14: note: int List<T>::locaton(const T&) [with T = int]
..\src\/List.h:22:14: note: int List<T>::remove(int, const T&) [with T = int]
Build error occurred, build is stopped
Time consumed: 230 ms.
c++自学了两周,上课学的是c,但是数据结构用c++描述,现在对于c++不是狠明白,求教。
作者: im_AK47 发布时间: 2011-12-25
注意书写错误问题:
virtual int locaton(const T& x) = 0;
与 int location(const T& x);
virtual int locaton(const T& x) = 0;
与 int location(const T& x);
作者: bottlebox 发布时间: 2011-12-25
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28