weixin_49967213 2021-09-08 16:23 采纳率: 100%
浏览 39
已结题

编译通过,运行的时候报错

**myExceptions.h**

#pragma once
#include<iostream>
using namespace std;
#include<string>

class illegalParameterValue {
public:
    illegalParameterValue(string theMessage = " Illegal Parameter Value") {
        message = theMessage;
    }

    void outputMessage() { cout << message << endl; }
private:
    string message;
};

class illegalIndex {
public:
    illegalIndex(string theMessage = " Illegal Index") {
        message = theMessage;
    }

    void outputMessage() { cout << message << endl; }
private:
    string message;
};

**linearList.hpp**

#pragma once
#include<iostream>
using namespace std;

template<class T>
class linearList {//抽象类
public:
    virtual ~linearList() {}//虚析构函数

    virtual bool empty() const = 0;
    //返回ture,当且仅当线性表为空

    virtual int size() const = 0;
    //返回线性表的元素个数

    virtual T& get(int theIndex) const = 0;
    //返回索引为theIndex的元素

    virtual int indexOf(const T& theElement) const = 0;
    //返回元素theElement第一次出现时的索引

    virtual void erase(int theIndex) = 0;
    //删除索引为theIndex的元素

    virtual void insert(int theIndex, const T& theElement) = 0;
    //把theElement插入线性表中索引为theIndex的位置上

    virtual void output(ostream& out) const = 0;
    //把线性表插入输出流out
};

arrayList.hpp

#pragma once
#include "linearList.hpp"
#include<algorithm>
#include<sstream>
#include "myExceptions.h"

template<class T>
class arrayList :public linearList<T>{
public:
    //构造函数,拷贝构造函数和析构函数
    arrayList(int initialCapacity = 10);

    arrayList(const arrayList<T>& theList);

    ~arrayList() { delete[] element; }

    //ADT方法
    bool empty() const { return listSize == 0; }

    int size() const { return listSize; }

    T& get(int theIndex)const;

    int indexOf(const T& theElement) const;

    void erase(int theIndex);

    void insert(int theIndex, const T& theElement);

    void output(ostream& out) const;

    //其他方法
    int capacity() const { return arrayLength; }
protected:
    void checkIndex(int theIndex)const;
    //若索引theIndex无效,则抛出异常

    T* element;//存储线性表元素的一维数组
    int arrayLength;//一维数组的容量
    int listSize;//线性表的元素个数
};

template<class T>
arrayList<T>::arrayList(int initialCapacity) {
    if (initialCapacity < 1) {
        ostringstream s;//包含头文件<sstream>
        s << "Initial capacity = " << initialCapacity << "must be > 0";
        throw illegalParameterValue(s.str());
    }
    arrayLength = initialCapacity;
    element = new T[arrayLength];
    listSize = 0;
}

template<class T>
arrayList<T>::arrayList(const arrayList<T>& theList) {
    arrayLength = theList.arrayLength;
    listSize = theList.listSize;
    element = new T[arrayLength];
    copy(theList.element, theList.element + listSize, element);
}

template<class T>
void arrayList<T>::checkIndex(int theIndex)const {
    //确定索引theIndex在0和arrayList - 1之间
    if (theIndex < 0 || theIndex > listSize - 1) {
        ostringstream s;
        s << "index = " << theIndex << " size = " << listSize;
        throw illegalIndex(s.str());
    }
}

template<class T>
T& arrayList<T>::get(int theIndex)const {
    //返回索引为theIndex的元素
    //若此元素不存在,则抛出异常
    checkIndex(theIndex);
    return element[theIndex];
}

template<class T>
int arrayList<T>::indexOf(const T& theElement) const {
    //返回元素theElement第一次出现时的索引
    //若该元素不存在,则返回-1
    /*for (int i = 0; i < arrayList; i++) {
        if (element[i] == theElement) {
            return i;
            break;
        }
    }*/

    //查找元素theElement
    int theIndex = (int)(find(element, element + listSize, theElement) - element);

    //确定元素theElement是否找到
    if(theIndex == listSize)
        return -1;
    return theIndex;
}

template<class T>
void arrayList<T>::erase(int theIndex) {
    //删除索引为theIndex的元素
    //若此元素不存在,则抛出异常illegalIndex

    checkIndex(theIndex);
    /*for (int i = theIndex; i < listSize - 1; i++) {
        int temp = element[i];
        element[i] = element[i + 1];
        element[i + 1] = temp;
    }
    listSize--;*/

    //有效索引,移动其索引大于theIndex的元素
    copy(element + theIndex + 1, element + listSize, element + theIndex);

    element[--listSize].~T();//调用析构函数
}

template<class T>
void changeLength1D(T* a, int oldLength, int newLength) {
    if (newLength < 0) {
        throw illegalParameterValue("new length must be >= 0");
    }
    
    T* temp = new T[newLength];//新数组
    int number = min(oldLength, newLength);//需要复制的元素个数
    copy(a, a + number, temp);
    delete[] a;//释放老数组的内存空间
    a = temp;
}

template<class T>
void arrayList<T>::insert(int theIndex, const T& theElement) {
    //把theElement插入线性表中索引为theIndex的位置上
    //if (theIndex < 0 || theIndex > listSize) {
    //    //无效索引
    //    ostringstream s;
    //    s << "index = " << theIndex << " size = " << listSize;
    //    throw illegalIndex(s.str());
    //}

    //有效索引,确定数组是否已满
    if (listSize == arrayLength) {
        changeLength1D(element, arrayLength, arrayLength * 2);
        arrayLength *= 2;
    }

    //把元素向右移动一个位置
    copy_backward(element + theIndex, element + listSize, element + listSize + 1);

    element[theIndex] = theElement;
}

template<class T>
void arrayList<T>::output(ostream& out) const {
    copy(element, element + listSize, ostream_iterator<T>(cout, " "));
}

template<class T>
ostream& operator<<(ostream& out, const arrayList<T>& x) {
    x.output(out);
    return out;
}

**main.cpp**

#include"arrayList.hpp"

int main()
{
    //创建两个容量为100的线性表
    linearList<int>* x = new arrayList<int>(100);
    arrayList<int> y(100);
    cout << y.get(2) << endl;

    system("pause");
    return 0;
}

img

img

  • 写回答

3条回答 默认 最新

  • 记绘衣 2021-09-08 16:25
    关注

    它不是说异常没接收吗?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月8日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效