| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1320 人关注过本帖
标题:编写了一个动态数组程序,编译无错误,但是无法运行,请前辈帮忙看看。。主 ...
只看楼主 加入收藏
瑞锋online
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2016-10-19
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
编写了一个动态数组程序,编译无错误,但是无法运行,请前辈帮忙看看。。主要是array.cpp
//array.h
#ifndef ARRAY_H
#define ARRAY_H
using namespace std;

//错误类型集合,共有三种类型的错误:数组大小错误、内存分配错误和下标越界
enum ErrorType {
    invalidArraySize,
    memoryAllocationError,
    indexOutOfRange
};

class Array {
public:
    Array(int sz = 50); //构造函数
    Array(const Array& a); //复制构造函数
    ~Array(); //析构函数
    void setElement(int n, int value); //设置第n个元素(数组下标为n-1)的值为value
    int& getElement(int n); //取第n个元素(数组下标为n-1)
    int arraySize() const; //返回数组大小
    void resize(int sz); //动态改变数组的大小
private:
    int* alist; //int类型指针,用于存放动态分配的数组内存首地址
    int size; //数组大小
    void handleError(ErrorType error,int badIndex=0) const; //错误处理函数
};

#endif
#include <iostream>
#include <string>
#include "array.h"
using namespace std;

//错误信息
string errorMsg[] = {
    "Invalid array size",
    "Memory allocation error",
    "Invalid index: "
};

//错误处理函数
void Array:: handleError(ErrorType error, int badIndex) const {
    cout << errorMsg[error]; //根据错误类型,输出相应错误信息
    if (error == indexOutOfRange)
        cout << badIndex; //如果是下标越界错误,输出错误的下标值
    cout << endl;
    exit(-1);
}

//构造函数
Array::Array(int sz) {
    size = sz;
    int *alist = new int[sz];
    if(alist == NULL)
        handleError(memoryAllocationError);
    cout << "Calling of constructor" << endl;
}

//析构函数
Array::~Array() {
    delete[] alist;
}

//复制构造函数
Array::Array(const Array &a) {
    size = a.size;
    int *alists = new int[size];
    for(int i=0; i < size; ++i)
         alists[i] = a.alist[i];
}
//设置数组下标为n处元素的值为value
void Array::setElement(int n, int value) {
    alist[n] = value;
}

//取数组下标为n的元素的值
int& Array::getElement(int n) {
    return alist[n];
}

//返回数组大小
int Array::arraySize() const {
    return size;
}

//将数组大小修改为sz
void Array::resize(int sz) {
    if(sz <= 0) //检查是否sz<= 0
        handleError(invalidArraySize);
    if(sz == size) //如果size==sz,则退出本函数
        return;

    int *newlist = new int[sz]; //申请新的数组内存
    if(newlist == NULL) //内存申请是否成功?
        handleError(memoryAllocationError);
     
    for(int i = 0; i < sz; ++i)
        newlist[i] = alist[i];
    delete[] alist;
   
    alist = newlist;
    delete newlist;
    size = sz;
}
#include <iostream>
#include<ctime>
#include "array.h"

using namespace std;

int main() {
    Array a(10);
    srand(time(0));

    for(int i = 0; i < 10; ++i)
        a.setElement(i+1, 1 + rand() % 9);

    cout << "数组大小:" << a.arraySize() << endl;
    cout << "数组a的元素值:";
    for( i = 0; i < a.arraySize(); ++i)
        cout << a.getElement(i+1) << " ";
    cout << endl;

    cout << "调整数组a的大小为原来的2倍: " << endl;
    a.resize( a.arraySize() * 2 );
    cout << "调整大小后数组的大小: " << a.arraySize() << endl;

    cout << "用复制构造函数,用数组a初始化b" << endl;
    Array b(a);

    cout << "修改数组a的最后一个元素值为100" << endl;
    a.setElement(a.arraySize(), 100);
    cout << "数组a的值: ";
    for( i = 0; i < a.arraySize(); ++i)
        cout << a.getElement(i+1) << " ";
    cout << endl;

    cout << "数组b的元素值: ";
    for( i = 0; i < b.arraySize(); ++i)
        cout << b.getElement(i+1) << " ";
    cout << endl;
   
    //错误处理测试
    //i=a.getElement(a.arraySize());
    a.resize(-10);

    cout<<"Quit...."<<endl;
    return 0;
}


[此贴子已经被作者于2017-10-22 23:41编辑过]

搜索更多相关主题的帖子: 数组 错误 array int cout 
2017-10-22 23:40
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
“编译无错误,但是无法运行” --- 听不懂。实在不行,你看看 std::vector 是怎么实现的
2017-10-23 10:47
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:10 
Array::Array(int sz) {
    size = sz;
    int *alist = new int[sz];
    if(alist == NULL)
        handleError(memoryAllocationError);
    cout << "Calling of constructor" << endl;
}
私有变量里面已经有alist了,你的备注也说明了功能。构造函数里面不应该另外定义一个alist指针,不仅与成员同名,而且只是在该模块内可用,等于是个让程序可能崩溃的定时炸弹。
直接alist= new int[sz]就行了。
另一个构造函数也有同样问题。其他没细看,不知有没有别的错。
2017-10-25 22:47
快速回复:编写了一个动态数组程序,编译无错误,但是无法运行,请前辈帮忙看看。 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015319 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved