可动态调整大小的C ++字符串数组的类

时间:2014-07-28 09:20:52

标签: c++

我需要根据给定的DynamicArray文件创建一个名为main.cpp的类。但是我不能让它发挥作用。程序崩溃了。我认为应该是构造函数问题。请参阅以下代码并附注释:

main.ccp

#include <iostream>
#include <dynamicarray.h>

using namespace std;

int main()
{
    DynamicArray* arr=new DynamicArray(ARRAY_SIZE);
    for (int i=0; i<25; i++) {
        arr->add("test");
    }
    for (int j=0; j<10; j++){
        arr->remove();
    }
    delete [] arr;
    arr=NULL;
    return 0;
}

dynamicarray.h

#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#define ARRAY_SIZE 5
#include <iostream>

using namespace std;

class DynamicArray
{
    DynamicArray* darr;
    int del;
    int occSize;
    int totSize;
public:
    DynamicArray();
    ~DynamicArray();
    DynamicArray(int);
    void add(string);
    void remove();
    int totalSize();
    int occupiedSize();
    DynamicArray operator= (string);
    DynamicArray operator= (int);
};

#endif // DYNAMICARRAY_H

dynamicarray.cpp

#include "dynamicarray.h"

DynamicArray::DynamicArray()
{
    darr=new DynamicArray[ARRAY_SIZE];
    del=0;
    occSize=0;
    totSize=ARRAY_SIZE;
}

DynamicArray::DynamicArray(int n){
    darr=new DynamicArray[n];
    del=0;
    occSize=0;
    totSize=ARRAY_SIZE;
}

DynamicArray::~DynamicArray()
{
    delete [] darr;
    darr=NULL;
}

//adding s to the next available slot in the array. When the array is full,
//add() will grow the array by another ARRAY_SIZE
void DynamicArray::add(string s){
    if (occSize<totSize){
        darr[occSize]=s;
        occSize++;
    }else{
        totSize=totSize+ARRAY_SIZE;
        DynamicArray* temp=new DynamicArray[totSize];
        for (int i=0; i<occSize; i++){
            temp[i]=darr[i];
        }
        darr=temp;
        delete [] temp;
        temp=NULL;
        darr[occSize]=s;
        occSize++;
    }
}

//remove the element where the index is poining to in the array.
//when there are more than ARRAY_SIZE empty slots left, the delete()
//will shrink the array by another ARRAY_SIZE
void DynamicArray::remove(){
    darr[del]=NULL;
    del++;
    occSize--;
    if (del>5){
        DynamicArray* temp=new DynamicArray[totSize-ARRAY_SIZE];
        int j=del;
        for (int i=0; i<occSize; i++){
            temp[i]=darr[del];
            j++;
        }
        darr=temp;
        del=0;
        delete [] temp;
        temp=NULL;
    }
}

//returning the current max capacity size of the array
int DynamicArray::totalSize(){
    return totSize;
}

//returning the number of occupied slots in the array
int DynamicArray::occupiedSize(){
    return occSize;
}

DynamicArray DynamicArray::operator= (string s){
    *this=s;
    return *this;
}

DynamicArray DynamicArray::operator= (int n){
    *this=n;
    return *this;
}

1 个答案:

答案 0 :(得分:2)

当然它会崩溃,就像在DynamicArray的构造函数中一样,你创建了DynamicArray的新实例,它调用了构造函数,而构造函数又创建了DynamicArray的新实例......这种无限递归将导致堆栈溢出,导致程序崩溃。

如果DynamicArray应该是动态数组(或向量)的字符串,那么它应该包含一个&#34;数组&#34;字符串,std::string个对象或指针。像

class DynamicArray
{
private:
    char** darr;  // The actual array of data

    ...
};

DynamicArray::DynamicArray(int n)
{
    darr = new char*[n];
    ...
}