C++ C++;Console Application1.exe已触发断点
当我试图设置C++ C++;Console Application1.exe已触发断点,c++,breakpoints,C++,Breakpoints,当我试图设置 cub.SetArray(cube); 我犯了一个错误 Console Application1.exe has triggered a breakpoint 我做错了什么?当我尝试调试cub->cubesarray时,我得到的大小是-84215051。我不明白为什么。这是我所有的密码 class Cube{ public: static const int Change_ARRAY = 5; private: string color; int s
cub.SetArray(cube);
我犯了一个错误
Console Application1.exe has triggered a breakpoint
我做错了什么?当我尝试调试cub->cubesarray
时,我得到的大小是-84215051。我不明白为什么。这是我所有的密码
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();
}
}
类多维数据集{
公众:
静态常量int Change_数组=5;
私人:
字符串颜色;
整数大小;
int*墙壁;
int n;//数组的当前大小
int maximumsize;//数组的最大大小
空隙增加(整数倍);
公众:
立方体(int-maximumsize=0);
~Cube();
空心墙(内墙);
void SetColor(字符串颜色);
void SetSize(int size);
字符串GetColor(){return color;}
intgetwalls(inti){返回墙[i];}
int GetSize(){return size;}
int GetN(){return n;}
};
多维数据集::多维数据集(int maximumsize):n(0),maximumsize(maximumsize),size(size),walls(NULL){
如果(最大大小>0){
墙=新整型[最大尺寸];
}
}
多维数据集::~Cube(){
如果(墙){
删除[]面墙;
}
}
void多维数据集::增加(整数多){
如果(多个>最大大小){
int*最新=新int[多];
对于(int i=0;isize=size;
}
类立方阵{
公众:
静态常量int Change_数组=5;
私人:
立方体*立方阵;
int currentsize;//数组的当前大小
int maxsize;//maximumsize
无效变更(int kk);
公众:
立方阵(int-maxsize=1);
~CubesArray();
void集合数组(立方体c);
多维数据集GetArray(int ind){返回立方数组[ind];}
int GetCsize(){return currentsize;}
};
CubesArray::CubesArray(int-maxsize):CubesArray(NULL)、currentsize(0)、maxsize(maxsize){
如果(最大尺寸>0){
立方体阵列=新立方体[maxsize];
}
}
立方阵::~CubesArray(){
if(立方阵){
删除[]立方阵;
}
}
void立方体阵列::更改(int kk){
如果(kk>maxsize){
立方体*newarr=新立方体[kk];
对于(int i=0;i颜色>>大小;
立方体。设置大小(大小);
立方体。设置颜色(颜色);
不能改变:
if(currentsize = maxsize)
致:
此外,这是您真正的问题:
在类多维数据集中没有复制构造函数
,因此每当您按值发送多维数据集
实例(例如,cub.SetArray(多维数据集)
)时,墙
数组不会被正确复制
您必须按以下方式定义它:
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];
}
}
顺便说一句,在复制构造函数中,您只需调用赋值运算符(删除编码冗余):
您的多维数据集类违反了三条规则。请看这里:
void CubesArray::SetArray(Cube cub){ // calls copy constructor
该调用创建了多维数据集类的副本。多维数据集类不可安全复制。请查看此内容并向下滚动到“管理资源”部分:
您应该按引用或常量引用传递多维数据集,而不是按值传递。这样做可能会更正您现在遇到的错误,但您的类仍然是错误的。仅供参考,std::vector walls;Cube
中的和std::vector
将使大量此代码消失。size(size)
在初始值设定项列表中是错误的。我在想大小(0)
?我知道,但我需要找出动态阵列的问题。从我提到的大小()问题开始,然后按照您的方式找到下面的答案(可能已经包括该问题)。你能解释一下在类Cube
中size
和n
的具体用途以及为什么两者都需要吗?不,初始值设定项中的大小(size)是可以的(我尝试更改为大小(0),但仍然遇到同样的问题).我认为数组不能以某种方式增加?谢谢,但我仍然发现控制台应用程序1.exe触发了breakpoint@barakmanos确实,您希望执行完全相反的操作。例如,使用by value赋值运算符参数并出于多种原因使用,其中包括异常保护。@WhozCraig:谢谢。您能详细说明一下吗关于上述复制构造函数/赋值运算符方案中的潜在缺陷?我一直使用此方案,因此我想知道概念上(或实际上)是什么错误。再次感谢…@barakmanos它要求对象必须先进行默认构造,然后进行赋值。您希望避免默认构造,而是使用复制构造函数,然后进行交换。这没有什么坏处,也有巨大的好处。顺便说一句,异常安全性在上行列表的顶部附近。链接I“提供”在描述它时比我在一篇简单的评论文章中所做的要公正得多。@WhozCraig,感谢演示:)…首先,我仍然没有看到我的方案的缺点。其次,我看到了你建议的方案的一些缺点:1.将参数按值传递给赋值运算符(这意味着对复制构造函数的额外调用和堆栈中的额外对象)。2.对STL的依赖性(或std
,无论您以何种方式调用它)。
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;
}
void CubesArray::SetArray(Cube cub){ // calls copy constructor