我查看了围绕glibc检测到的错误的其他几个问题,但是我的代码看起来有点不同。我相信错误是在我的四面体类中,因为如果我将它与调用它一起注释,代码似乎运行正常。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
//---------------------------------------------------------------------
// this is the most basic class, which everything below it includes the name, color and speed.
class Shape {
protected:
char *name;
char *color;
double shape_speed;
public:
Shape(const char *shapec, double speed){
char nametmp[] = "Located in shape";
double const pi = 3.14159;
int length = strlen(shapec);
color = new char[length +1];
strcpy(color,shapec);
length = strlen(nametmp);
name = new char[length +1];
strcpy(name,nametmp);
shape_speed = speed;
}
virtual void print() {
cout << "name: " << name << endl;
cout << "color: " << color << endl;
cout << "speed of shape: " << shape_speed << "\n"<< endl;
}
virtual ~Shape() {
delete [] name;
delete [] color;
cout << "shape deconstructor called" << endl;
}
};
// This is the class for the three dimensional shapes. this includes the coordinates for the center which are inputed in main
class Shape3d: public Shape {
protected:
double x3dc; // center for 3d x coordinate
double y3dc; // center for 3d y coordinate
double z3dc; // center for 3d z coordinate
public:
Shape3d(const char *shapec, double speed,double x, double y, double z):Shape(shapec, speed){
Shape(shapec, speed);
x3dc = x;
y3dc = y;
z3dc = z;
char nametmp[] = "Located in 3d shape";
int length = strlen(nametmp);
name = new char[length +1];
strcpy(name,nametmp);
}
virtual void print() {
cout << "name: " << name << endl;
cout << "color: " << color << endl;
cout << "speed of 3d shape: " << shape_speed << endl;
cout << "center of 3d shape: (" << x3dc << "," << y3dc<< "," << z3dc << ")" << "\n"<<endl;
}
virtual ~Shape3d() {
delete [] name;
delete [] color;
cout << "3d shape deconstructor called" << endl;
}
};
// This is the class for the three dimensional tetrahedron. This adds the volume of the shape.
class Shape3dtet: public Shape3d {
protected:
double edge;
double volume;
double calculate_volume() {
volume = pow(edge,3.0)/sqrt(72.0);
return volume;
}
public:
Shape3dtet(const char *shapec, double speed,double x, double y, double z, double e):Shape3d(shapec, speed,x,y,z){
char nametmp[] = "Located in 3d tetrahedron";
int length = strlen(nametmp);
name = new char[length +1];
strcpy(name,nametmp);
edge = e;
calculate_volume();
}
virtual void print() {
cout << "name: " << name << endl;
cout << "color: " << color << endl;
cout << "speed of tetrahedron: " << ": "<< shape_speed << endl;
cout << "volume of tetrahedron: " << volume << endl;
cout << "center of tetrahedron: (" << x3dc << "," << y3dc<< "," << z3dc << ")" << "\n"<<endl;
}
virtual ~Shape3dtet() {
delete [] name;
delete [] color;
cout << "3d tetrahedron deconstructor called" << endl;
}
};
int main () {
Shape basic("blue", 5.3);
Shape3d threedim("smaragdine",22.8,3,3,3 );
Shape3dtet tet("carnation pink",1,2,3,4,5);
basic.print();
threedim.print();
tet.print();
return 0;
}
答案 0 :(得分:2)
您正在致电
delete [] name;
delete [] color;
基类析构函数中的以及所有派生类的析构函数。这会导致双重删除错误。
您肯定需要从派生类的析构函数中删除这些调用。此外,您需要遵循The Rule of Three并实现正确的复制构造函数和复制赋值运算符。否则,你很快就会遇到同样的问题。
您还可以使用std::string
代替char*
来大幅简化代码。在这种情况下,您无需担心The Rule of Three。