C++ 控制台 Application1.exe 已触发断点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22125539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Console Application1.exe has triggered a breakpoint
提问by user3369351
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
我做错了什么?当我尝试调试时,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();
}
}
回答by barak manos
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)
.
您在 中没有复制构造函数class Cube
,因此walls
每当您Cube
按值发送实例时,数组都不会被正确复制,例如,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
.
并且您在 中没有赋值运算符class Cube
,因此walls
每当您将一个Cube
实例分配给另一个实例时,数组都不会正确复制,例如,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;
}
回答by PaulMcKenzie
Your Cube class violates the rule of three. Look here:
您的 Cube 类违反了三规则。看这里:
void CubesArray::SetArray(Cube cub){ // calls copy constructor
That call creates a copy of your Cube class. Your Cube class is not safely copyable. Please see this and scroll down to the Managing Resourcessection: What is The Rule of Three?
该调用会创建 Cube 类的副本。您的 Cube 类不能安全地复制。请查看此内容并向下滚动到管理资源部分: 什么是三法则?
You should pass Cube by reference or const reference, not by value. Doing so maycorrect the error you're having now, but still, your class is faulty.
您应该通过引用或常量引用来传递 Cube,而不是通过值。这样做可能会纠正您现在遇到的错误,但您的课程仍然有问题。