Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Console Application1.exe has triggered a breakpoint

when I try to set

cub.SetArray(cube);

I get an error

Console Application1.exe has triggered a breakpoint 

What I'm doing wrong? When I try to debug cub -> cubesarray I get size -842150451. I don't understand why.Here's my all code

class Cube{
public:
    static const int Change_ARRAY = 5;

private:
    string color;
    int size;
    int *walls;
    int n; // current size of array
    int maximumsize; // maximum size of array
    void Increase(int many);
public:
    Cube(int maximumsize = 0);
    ~Cube();
    void SetWalls(int wall);
    void SetColor(string color);
    void SetSize(int size);

    string GetColor(){return color;}
    int GetWalls(int i){return walls[i];}
    int GetSize(){return size;}

    int GetN(){return n;}
};

Cube::Cube(int maximumsize):n(0), maximumsize(maximumsize), size(size), walls(NULL){
    if(maximumsize > 0){
        walls = new int[maximumsize];
    }
}

Cube::~Cube(){
    if(walls){
        delete [] walls;
    }
}

void Cube::Increase(int many){
    if(many > maximumsize){
        int *newest = new int[many];
        for(int i=0; i<n; i++)
            newest[i] = walls[i];
        delete [] walls;
        walls = newest;
        maximumsize = many;
    }else if( many < maximumsize){
        int *newest = new int[many];
        for(int i=0; i<many; i++)
            newest[i] = walls[i];
        delete [] walls;
        walls = newest;
        n = maximumsize = many;
    }
}

void Cube::SetWalls(int wall){
    if(n == maximumsize) Increase(n + Change_ARRAY);
    walls[n] = wall;
    n++;
}

void Cube::SetColor(string color){
    this->color = color;
}

void Cube::SetSize(int size){
    this->size = size;
}

class CubesArray{
public:
    static const int Change_Array = 5;
private:
    Cube *cubesarray;
    int currentsize; // current size of array
    int maxsize; // maximumsize
    void Change (int kk);
public:
    CubesArray(int maxsize = 1);
    ~CubesArray();

    void SetArray(Cube c);
    Cube GetArray(int ind){return cubesarray[ind];}
    int GetCsize(){return currentsize;}
};

CubesArray::CubesArray(int maxsize):cubesarray(NULL), currentsize(0), maxsize(maxsize){
    if(maxsize > 0){
        cubesarray = new Cube[maxsize];
    }
}

CubesArray::~CubesArray(){
    if(cubesarray){
        delete [] cubesarray;
    }
}

void CubesArray::Change(int kk){
    if(kk > maxsize){
        Cube *newarr = new Cube[kk];
        for(int i=0; i<currentsize; i++)
            newarr[i] = cubesarray[i];
        delete [] cubesarray;
        cubesarray = newarr;
        maxsize = kk;
    }if(kk < maxsize){
        Cube *newarr = new Cube[kk];
        for(int i=0; i<kk; i++)
            newarr[i] = cubesarray[i];
        delete [] cubesarray;
        cubesarray = newarr;
        currentsize = maxsize = kk;
    }
}

void CubesArray::SetArray(Cube cub){
    if(currentsize = maxsize) Change(currentsize + Change_Array);
    cubesarray[currentsize] = cub;
    currentsize++;
}

void Read(CubesArray & cub);

int main(){
    CubesArray cub;

    Read(cub);

    system("pause");
    return 0;
}

void Read(CubesArray & cub){
    string color;
    int size;
    int i=0;
    Cube cube;
    ifstream fd(Data);
    while(!fd.eof()){
        fd >> color >> size;
        cube.SetSize(size);
        cube.SetColor(color);
        cout << cube.GetColor() << " " << cube.GetSize() << " ";
        while(fd.peek() != '\n' && !fd.eof()){
            int w;
            fd >> w;
            cube.SetWalls(w);
            cout << cube.GetWalls(i) << " ";
            cub.SetArray(cube); // when I set cube to cub I get this error!!!
            i++;
        }
        cout << endl;
        fd.ignore();
    }
}
like image 433
user3369351 Avatar asked Mar 02 '14 08:03

user3369351


1 Answers

Change:

if(currentsize = maxsize)

To:

if(currentsize == maxsize)

In addition, here is your real problem:

You have no copy-constructor in class Cube, so the walls array is not properly copied whenever you send a Cube instance by value, e.g., cub.SetArray(cube).

You must define it as follows:

Cube::Cube(const Cube& cube):n(cube.n),maximumsize(cube.maximumsize),size(cube.size),wall(NULL)
{
    if (maximumsize > 0)
    {
        walls = new int[maximumsize];
        for (int i=0; i<maximumsize; i++)
            wall[i] = cube.wall[i];
    }
}

And you have no assignment-operator in class Cube, so the walls array is not properly copied whenever you assign one Cube instance into another, e.g., cubesarray[currentsize] = cub.

You must define it as follows:

Cube& Cube::operator=(const Cube& cube)
{
    n = cube.n;
    maximumsize = cube.maximumsize;
    size = cube.size;
    wall = NULL;
    if (maximumsize > 0)
    {
        walls = new int[maximumsize];
        for (int i=0; i<maximumsize; i++)
            wall[i] = cube.wall[i];
    }
    return *this;
}

BTW, in the copy-constructor, you can simply call the assignment-operator (remove coding redundancy):

Cube::Cube(const Cube& cube)
{
    if (this != &cube)
        *this = cube;
}
like image 179
barak manos Avatar answered Oct 04 '22 06:10

barak manos