gpt4 book ai didi

C++ 控制台 Application1.exe 已触发断点

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:23:31 25 4
gpt4 key购买 nike

当我尝试设置时

cub.SetArray(cube);

我得到一个错误

Console Application1.exe has triggered a breakpoint 

我做错了什么?当我尝试调试 cub -> cubesarray 时,我得到大小 -842150451。我不明白为什么。这是我的所有代码

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();
}
}

最佳答案

改变:

if(currentsize = maxsize)

收件人:

if(currentsize == maxsize)

另外,这是你真正的问题:

class Cube 中没有复制构造函数,因此当您按值发送 Cube 实例时,walls 数组未正确复制,例如,cub.SetArray(cube)

您必须按如下方式定义它:

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];
}
}

并且您在 class Cube 中没有赋值运算符,因此每当您将一个 Cube 实例分配到其中时,walls 数组都不会正确复制另一个,例如,cubesarray[currentsize] = cub

您必须按如下方式定义它:

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;
}

顺便说一句,在复制构造函数中,您可以简单地调用赋值运算符(删除编码冗余):

Cube::Cube(const Cube& cube)
{
if (this != &cube)
*this = cube;
}

关于C++ 控制台 Application1.exe 已触发断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com