Hello all,

I am having a bit of a problem with a part of my assignment I hope you can help me out with. I created an abstract baseclass called Geometry and 3 derived classes called Box, Sphere, and Cylinder. I am having a few problems though and I hope you can help me, as they seem simple, but are causing me a headache.

First, i declare a dynamic array of type Geometry, and from the different array parts I create new Box, Sphere, Cylinders, as so:

Geometry *geometryArray[GEOMETRY_COUNT];

geometryArray[0] = new Box("Box 1", 1, 2, 3);
geometryArray[1] = new Box("Box 2", 3, 4, 5);
geometryArray[2] = new Box("Box 3", 5, 6, 7);
geometryArray[3] = new Sphere("Sphere 1", 2);
geometryArray[4] = new Sphere("Sphere 2", 3);
geometryArray[5] = new Sphere("Sphere 3", 4);
geometryArray[6] = new Cylinder("Cylinder 1", 3, 4);
geometryArray[7] = new Cylinder("Cylinder 2", 4, 5);
geometryArray[8] = new Cylinder("Cylinder 3", 5, 6);
geometryArray[9] = new Cylinder("Cylinder 3", 5, 6);

report(geometryArray);

delete [] geometryArray;

the program runs correctly, and everything is dandy. but when it goes to delete [] geometryArray; the program crashes, and I cant figure out why. Do I just need to delete the individual parts of the array seperately? I only have one destructor that is located in my Geometry.h and Geometry.cpp, and it looks like this:

Geometry.h

~Geometry();

Geometry.cpp

Geometry::~Geometry(){

    delete[] Name;
    delete[] Type;
}

the delete[] Name and delete[] Type are to delete dynamically allocated char arrays created to give the objects a type and a name.

I do not have destructors in my other derived classes, i tried to add them, but I still get the same error. Do you know why?

Thank you for your help.

just in case someone asks, here is my full code from the .cpp's of Geometry.cpp and Box.cpp, as box and sphere and cylinder are relatively the same:

Geometry.cpp

//Geometry--
#include "Geometry.h"
#include <iostream>

using namespace std;

Geometry::Geometry(char *name, char *type){

    Name = new char[strlen(name)+1];
    strcpy_s(Name, strlen(name)+1, name);

    Type = new char[strlen(type)+1];
    strcpy_s(Type, strlen(type)+1, type);
}

Geometry::~Geometry(){

    delete[] Name;
    delete[] Type;
}

char *Geometry::getName(){

    return Name;
}

char *Geometry::getType(){

    return Type;
}

/**ostream &operator<<(ostream &os, const Geometry &obj){

    os << "Type: " << obj.getType() << endl << "Name: " << obj.getName() << endl << "Volume: " << obj.computeVolume() << endl << "Surface Area: " << obj.computeSurface();

    return os;
}*/


//BOX--
#include "Box.h"
#include <iostream>

using namespace std;

double Box::computeVolume(){

    m_Volume = m_Length * m_Width * m_Height;

    return m_Volume;
}

double Box::computeSurface(){

    m_AreaHW = m_Height * m_Width;
    m_AreaHL = m_Height * m_Length;
    m_AreaWL = m_Width * m_Length;

    m_SurfaceArea = (2 * m_AreaHW) + (2 * m_AreaHL) + (2 * m_AreaWL);

    return m_SurfaceArea;
}

> Do I just need to delete the individual parts of the array seperately?
the array geometryArray itself does not have a dynamic storage duration. (it was not allocated using new[] ).
therefore, delete [] geometryArray; is an error.
the contents of the array are pointers to objects allocated with a dynamic storage duration ( new ). you need delete on these pointers.

for( int i=0 ; i<GEOMETRY_COUNT ; ++i ) delete geometryArray[i] ;

note: Geometry requires a virtual destructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.