调用构造函数时带有数组对象的分割故障
这是基类
class Product{
protected:
string model;
double price;
int qty;
public:
Product() { // default constructor
model = "";
price = 0.00;
qty = 0;
}
//setter method:
void setProduct(string m, double p, int q){ // set the values of Product (model, price, and quantity)
model = m;
price = p;
qty = q;
}
//getter method:
string getProduct(){ // get the product by returning the string model (unique data in the list)
return model;
}
int countLength(); // functino to count the number of products in a list
int seqSearch(string item, int length); // sequential search to search thru the list
void add();
void insertAt(int length, string newModel, double newPrice, int newQty); // add() and insertAt method used to insert a product into a list
void del();
void removeAt(int loc, int length); // del() and removeAt() methods is used to remove a product from a list
void view(); // display all info in a list
void select(); // method to select a product/item in a list
void view(int location); // view a selected product in a list... overloading the view() method
~Product(){ // destructor
delete [] this;
}
};
这是派生类
class Computer: public Product{
public:
//constructors
Computer(){} // default constructor
Computer(string m, double p, int q); // overloading constructor for preset data
void view();
~Computer(){ // destructor
delete [] this;
}
};
class Accessory: public Product{
public:
//constructoirs
Accessory(){} // default constructor
Accessory(string m, double p, int q);
void view();
~Accessory(){ // destructor
delete [] this;
}
};
class Printer: public Product{
public:
//constructors
Printer(){} // default constructor
Printer(string m, double p, int q);
void view();
~Printer(){ // destructor
delete [] this;
}
};
class Other: public Product{
public:
//constructors
Other(){} // default constructor
Other(string m, double p, int q);
void view();
~Other(){ //destructor
delete [] this;
}
};
这是派生类的过载构造函数
Computer::Computer(string m, double p, int q){
this->setProduct(m, p, q);
}
Accessory::Accessory(string m, double p, int q){
this->setProduct(m, p, q);
}
Printer::Printer(string m, double p, int q){
this->setProduct(m, p, q);
}
Other::Other(string m, double p, int q){
this->setProduct(m, p, q);
}
这是对象声明
Computer *Computers = new Computer[maxSize];
Accessory *Accessories = new Accessory[maxSize];
Printer *Printers = new Printer[maxSize];
Other *Others = new Other[maxSize];
问题是当我调用对象[i] = class(字符串,double,int)时,这是一个分段故障,我猜
void presetData(){
Computers[0] = Computer("C1C1C1", 2000.00, 6);
Computers[1] = Computer("C2C2C2", 2500.00, 2);
Accessories[0] = Accessory("A1A1A1", 200.00, 6);
Printers[0] = Printer("P1P1P1", 300.00, 4);
Others[0] = Other("O1O1O1", 100.00, 1);
}
为什么我不能访问对象[i]并且它给出了细分故障?
This is the base class
class Product{
protected:
string model;
double price;
int qty;
public:
Product() { // default constructor
model = "";
price = 0.00;
qty = 0;
}
//setter method:
void setProduct(string m, double p, int q){ // set the values of Product (model, price, and quantity)
model = m;
price = p;
qty = q;
}
//getter method:
string getProduct(){ // get the product by returning the string model (unique data in the list)
return model;
}
int countLength(); // functino to count the number of products in a list
int seqSearch(string item, int length); // sequential search to search thru the list
void add();
void insertAt(int length, string newModel, double newPrice, int newQty); // add() and insertAt method used to insert a product into a list
void del();
void removeAt(int loc, int length); // del() and removeAt() methods is used to remove a product from a list
void view(); // display all info in a list
void select(); // method to select a product/item in a list
void view(int location); // view a selected product in a list... overloading the view() method
~Product(){ // destructor
delete [] this;
}
};
This is the derived classes
class Computer: public Product{
public:
//constructors
Computer(){} // default constructor
Computer(string m, double p, int q); // overloading constructor for preset data
void view();
~Computer(){ // destructor
delete [] this;
}
};
class Accessory: public Product{
public:
//constructoirs
Accessory(){} // default constructor
Accessory(string m, double p, int q);
void view();
~Accessory(){ // destructor
delete [] this;
}
};
class Printer: public Product{
public:
//constructors
Printer(){} // default constructor
Printer(string m, double p, int q);
void view();
~Printer(){ // destructor
delete [] this;
}
};
class Other: public Product{
public:
//constructors
Other(){} // default constructor
Other(string m, double p, int q);
void view();
~Other(){ //destructor
delete [] this;
}
};
Here is the overloaded constructors of derived classes
Computer::Computer(string m, double p, int q){
this->setProduct(m, p, q);
}
Accessory::Accessory(string m, double p, int q){
this->setProduct(m, p, q);
}
Printer::Printer(string m, double p, int q){
this->setProduct(m, p, q);
}
Other::Other(string m, double p, int q){
this->setProduct(m, p, q);
}
This is the objects declarations
Computer *Computers = new Computer[maxSize];
Accessory *Accessories = new Accessory[maxSize];
Printer *Printers = new Printer[maxSize];
Other *Others = new Other[maxSize];
The problem is when I call the Object[i] = Class(string, double, int) and it is a segmentation fault I guess
void presetData(){
Computers[0] = Computer("C1C1C1", 2000.00, 6);
Computers[1] = Computer("C2C2C2", 2500.00, 2);
Accessories[0] = Accessory("A1A1A1", 200.00, 6);
Printers[0] = Printer("P1P1P1", 300.00, 4);
Others[0] = Other("O1O1O1", 100.00, 1);
}
Why can't I access the Object[i] and it gives segmentation fault?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当临时对象
计算机(“ C1C1C1”,2000.00,6)
被销毁时,它确实delete [] this;
,但是 this 不指向使用new
的数组的第一个元素。这具有不确定的行为,您很幸运它崩溃了。
更糟糕的是,它在
product ::〜产品
中进行两次,然后在Computer :: 〜Computer
中进行一次。删除所有说
delete [] this;
的行,此东西不属于destructor,您从未见过任何执行此操作的示例。(除非这是您不应该做的事情的示例。)
然后在您喜欢的C ++书中阅读更多有关构造函数和损坏器以及如何处理动态分配的信息。
When the temporary object
Computer("C1C1C1", 2000.00, 6)
is destroyed, it doesdelete [] this;
, butthis
does not point to the first element of an array created withnew
.This has undefined behaviour, and you're lucky it crashes.
To make it even worse, it does this twice - once in
Product::~Product
and once inComputer::~Computer
.Remove all the lines that say
delete [] this;
- this thing does not belong in a destructor, and you have never seen any example that does it.(Unless it was an example of things that you shouldn't do.)
Then read some more about constructors and destructors, and how to work with dynamic allocation, in your favourite C++ book.