C++:试图通过指针和函数复制类时出现分段错误
C++:Segmentation Fault When Attempting to Copy a Class via pointers and functions
代码最后一步的目标是创建一个类的临时实例,在该类的私有数据成员上使用一个集,在实例保持在作用域中时打印出实例,然后使用一个负责指针和类实例的析构函数。我得到了分割错误,我已经得到它一段时间了,即使有很多变化,所以这让我非常沮丧。
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
//Class Definition
class Box{
//Private data members
private:
int height;
int width;
int depth;
char *name;
void pri_setname(char *n);
public:
//Public constructors and deconstructor
Box(int,int,int,char *);
Box(const Box &obj);
~Box();
//Public function prototypes
void width_set(int w);
void height_set(int h);
void depth_set(int d);
void name_set(char *n);
void name_print();
int volume_print();
void objCreateKeep(Box **, char *n);
void objCreateTmp(char *n);
};
//Default constructor
Box::Box(int h1 = 1,int w1 =1, int d1 =1,char *n1 = "Blue Box")
{
strcpy(name,n1);
height = h1;
width = w1;
depth = d1;
}
//Copy constructor
Box::Box(const Box &obj)
{
name = new char[25];
strcpy(name,obj.name);
height = obj.height;
width = obj.width;
depth = obj.depth;
}
//Destructor
Box::~Box()
{
delete [] name;
cout<<"Destructor invoked, name pointer is deallocated";
}
void Box::pri_setname(char *n)
{
strcpy(name,n);
}
//Set the width of Box()
void Box::width_set(int w)
{
width = w;
}
//Set the height of Box()
void Box::height_set(int h)
{
height = h;
}
//Set the depth of Box()
void Box::depth_set(int d)
{
depth = d;
}
//Set the name of Box()
void Box::name_set(char *n)
{
name = new char[30];
strcpy(name,n);
pri_setname(name);
}
//Prints the name of the box
void Box::name_print()
{
cout<<"Box Name: "<<name;
}
//Calculate and Print volume of Box()
int Box::volume_print()
{
int volume = 0;
volume = height * width * depth;
return volume ;
}
void Box::objCreateTmp(char *n)
{
Box tmp;
tmp.name_set(n);
tmp.name_print();
tmp.~Box();
}
void Box::objCreateKeep(Box **pp, char *n)
{
Box *p = new Box;
pp = &p;
p->objCreateTmp(n);
delete p;
}
int main(int argc, int argv[])
{
//Check for correct # of cmd line args
/*
if(argc != 3)
{
cout<<"Wrong number of arguments";
}
*/
/*
Box a;
a.height_set(argv[1]);
a.width_set(argv[2]);
a.depth_set(argv[3]);
Box b = a;
Box c = a;
//Set the names of box B and C
b.name_set("Red Box");
c.name_set("Orange Box");
a.name_print();
b.name_print();
c.name_print();
*/
Box *keep;
Box **pp;
keep->objCreateKeep(pp,"Blue Box");
keep->objCreateKeep(pp,"Red Box");
keep->objCreateKeep(pp,"Orange Box");
system("PAUSE");
return(0);
}
在行
pp = &p;
您将p
的address
分配给pp
当你delete
p时,你记得它住在哪里并不重要。。。
p->objCreateTmp(n);
delete p;
除了这里的其他答案:
tmp.~Box();
-编号
析构函数在超出作用域时被调用。
delete [] name;
在析构函数中被调用,现在它将被调用两次。令人不快的
相关文章:
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 模板,函数使用错误的构造函数来复制我的对象
- 使用 memcpy() 复制到 std::chrono::milliseconds 会给出错误 -Werror=clas
- C++复制函数重载导致"must be a nonstatic member function"错误
- 为什么我的代码在尝试复制字符数组时引发 C6386 错误?
- 将缓冲区复制到剪贴板 [换行错误]
- 尝试通过memcpy复制大尺寸浮点向量时的分割错误
- 使用复制构造函数的程序输出错误
- 为什么定义复制构造函数会给我错误:无法将类型 'obj&' 的非常量左值引用绑定到类型为"obj"的右值?
- 复制内存给出分段错误
- C:试图将指针值复制到另一个指针中,得到可修改的左值错误
- emplace_back和push_back给出错误'double free or corruption (fasttop)'尽管定义了复制和移动构造函数
- 为什么 std::atomic<std::string> 会给出微不足道的可复制错误?
- STL向量上出现奇怪的复制构造函数错误
- 不可复制类数据成员的统一初始化导致gcc错误
- 错误:使用复制和交换习惯用法的交换函数中"operator="的重载不明确
- 编译器错误:只需要复制省略,但似乎需要移动构造函数(通过编译器)
- 瓦尔格林德未显示数组复制错误
- 字符数组复制错误
- memcpy问题;复制错误的块和多次