博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++数据结构之线性表的顺序结构实现(cannot declare variable ‘..‘ to be of abstract type ‘....‘处理)
阅读量:2339 次
发布时间:2019-05-10

本文共 3677 字,大约阅读时间需要 12 分钟。

线性表的顺序结构实现

代码实现

  • 抽象基类AbstractList.h文件
template
class AbstractList{
public: virtual bool isEmpty() const=0; virtual int length() const=0; virtual bool findElement(int k,T& x)const =0; //find the element whose index is k and assign the result to the x virtual int searchElement(const T& x)const =0; //search the x element and return its index virtual AbstractList
& delete(int k,T& x)=0; //delete the x element ,return the deleted Linklist and return the deleted element virtual AbstractList
& insert(int k,const T& x)=0; //insert the element at the position whose index is k virtual void output() const=0;};
分析与总结
  • 使用“virtual … =0”的方式去声明纯虚函数,而在结尾加上对应的const,则表明该成员函数并不能够对类的成员进行修改

  • 对于大型的数据量,要尽量调用的引用来进行传值,减少内存消耗

  • 如果不需要修改形参的值,可在形参前面加上const,声明为的常量

  • LinerList.h文件

#ifndef _LINERLIST_H#define _LINERLIST_H#include "AbstractList.h"#include 
using namespace std;template
class LinerList:public AbstractList
{
public: LinerList(int maxSize=10); ~LinerList(){
delete []element;} bool isEmpty()const{
return length==0;}; int getLength()const {
return length;}; bool findElement(int k,T& x)const; //find the element whose index is k and assign the result to the x int searchElement(const T& x)const; //search the x element and return its index AbstractList
& Delete(int k,T& x); //delete the x element ,return the deleted Linklist and return the deleted element AbstractList
& insert(int k,const T& x); //insert the element at the position whose index is k void output() const; AbstractList
& add(const T& x);private: int length; int maxSize; T* element;};template
LinerList
::LinerList(int maxSize){ element = new T[maxSize]; this->maxSize = maxSize; length = 0;}//find the element whose index is k and assign the result to the xtemplate
bool LinerList
::findElement(int k,T& x)const{ if(k<0 || k>length) { cout<<"error:out of index!"<
int LinerList
::searchElement(const T& x)const{ if(isEmpty()) return -1; //for the illegal value use illegal value to represent for! for(int i = 0;i < length;i++) { if(element[i] == x) { return ++i; //caution:the index of the LinerList is one more than array! } } return -1;}//delete the x element ,return the deleted Linklist and return the deleted elementtemplate
AbstractList
& LinerList
::Delete(int k,T& x){ if(findElement(k,x)) { for(int i = k; i < length; i ++) { element[i-1] = element[i]; } length --; return *this; } else { cout<<"out of the index"<
AbstractList
& LinerList
::insert(int k,const T& x){ if(!(length == 0 && k == 1)) { if(k>length || k<1) { cout<<"out of the index!"<
= k-1 && i >= 0; i --) { element[i + 1] = element[i]; } //insert the element in the targeted position } element[k - 1] = x; length ++; return *this;}template
void LinerList
::output() const{ cout<<"the length of the LinerList is:"<
<
< length;i ++) { cout<
<<" "; } cout<
AbstractList
& LinerList
::add(const T& x){ if(length == maxSize) { cout<<"out of memeory!add the element absortively"<
  • main测试文件
#include 
#include "LinerList.h"using namespace std;int main(){
LinerList
L(5); cout<<"length:"<
<

在这里插入图片描述

分析与总结

  • 引用是没有办法被修改的,而对引用使用赋值符号,并不是将修改引用的指向,是将等号的左边的对象的属性,完全复制到等号的右边的对象的属性。注意对等号的理解
  • 注意下标:对于使用者而言,下标是从1开始的,但是对于程序而言,下标是从0开始的!彼此转换,记得加一减一。用户输入的索引是从1开始的,程序员是从零开始,注意两者之间的转换
  • 学会使用异常处理机制,来替代单纯的输入输出语句
  • 编写程序的过程注意分步处理,按照情况进行分类

遇到的异常

cannot declare variable ‘…’ to be of abstract type ‘…’
  • 没有彻底实现所有的虚函数——漏了const的常量的声明
    • 只需要加上对应的const常量声明即可
      在这里插入图片描述
      在这里插入图片描述
udefined reference to ‘…’
  • 对于模板类而言,C++中部分编译器,不支持声明.h和实现.cpp分开写——只需要将之整合到一个文件中即可

在这里插入图片描述

在这里插入图片描述

转载地址:http://cwwvb.baihongyu.com/

你可能感兴趣的文章
drools使用记录
查看>>
阿里六面,挂在hrg,我真的不甘心!
查看>>
人生的康波周期,把握住一次,足以改变命运!
查看>>
互联网公司那些价值观-阿里巴巴
查看>>
去面试快手,问了我很多消息队列的知识!
查看>>
图解LeetCode No.18之四数之和
查看>>
402. Remove K Digits
查看>>
75. Sort Colors
查看>>
获取数组中前K小的数字
查看>>
数组heapify变为堆结构
查看>>
二叉树的非递归遍历
查看>>
218. The Skyline Problem
查看>>
Java PAT (Basic Level) Practice 写出这个数
查看>>
Python PAT (Basic Level) Practice 1016 部分A+B
查看>>
Python PAT (Basic Level) Practice 1006 换个格式输出整数
查看>>
Python PAT (Basic Level) Practice 1009 说反话
查看>>
Python PAT (Basic Level) Practice 1011 A+B 和 C
查看>>
Python PAT (Basic Level) Practice 1017 A除以B
查看>>
Python PAT (Basic Level) Practice 1042 字符统计
查看>>
spring dubbo 2.7.3 zookeeper 项目构建
查看>>