Reputation: 19
I am receiving the "Debug error" CRT detected that the application wrote to memory after end of heap. But I don't understand why and can't find any similar examples.
The full code is... Although I think it may just be a problem with main.
#include <iostream>
#include <cmath>
#include <array>
using namespace std;
// Declare global consts
const double pi = 3.1415926583;
// Base class Shape
class Shape{
protected:
double *sides;
public:
Shape(const int n){ //parameterized for n dimensional shape
sides = new double[n];
}
// need virtual destructor
virtual ~Shape(){
delete[] sides;
}
virtual double area() const = 0; // pure virtual function for area
virtual double volume() const = 0; // pure virtual function for volume
};
//////////////////////////////////////////////////////////
//Derived classes for 2D and 3D Shapes
class Shape2D : public Shape{ // inherit shape
protected:
int n = 2; //n denotes the number of dimensions
public:
// default constructor
Shape2D() :Shape(n){}
// param constructor
Shape2D(const double side1, const double side2) :Shape(n){
sides[0] = side1; sides[1] = side2;
}
virtual ~Shape2D(){} //virtual destructor
double volume() const { cout << "trying to calculate volume of 2d shape..." << endl; return 0; };
};
/////////////////////////////////////////////////////////////////////
//2D shapes
class Rectangle : public Shape2D{
public:
// constructors
Rectangle() :Shape2D() {}
Rectangle(const double side1, const double side2) :Shape2D(side1, side2){}
~Rectangle(){}
double area() const { return (sides[0] * sides[1]); }
};
int main(){
Shape **ShapePointer = new Shape*[2];
ShapePointer[0] = new Rectangle(2, 5);
ShapePointer[1] = new Rectangle(1, 3);
// clean up
delete ShapePointer[0];
delete ShapePointer[1];
delete[] ShapePointer;
system("pause");
return 0;
}
Upvotes: 1
Views: 2480
Reputation: 66230
Danger!!!
protected:
int n = 2; //n denotes the number of dimensions
public:
// default constructor
Shape2D() :Shape(n){}
n
is undefined when you initialize Shape(n)
Same problem with
Shape2D(const double side1, const double side2) :Shape(n)
If you can, define n
as static const
(or static constexpr
, because you tagged C++11)
protected:
static constexpr int n = 2; //n denotes the number of dimensions
Otherwise you should define a static const
/constexpr
variable (say nDef
), with value 2
, and initialize both n
and Shape()
with this constant.
Anyway, take in count that, now, Shape
(a base class for Shape2D
) is initialized before n
, that is a member of the class.
Upvotes: 4
Reputation: 131
the problem is the following two lines of the code:
int n = 2; //n denotes the number of dimensions
Shape2D(const double side1, const double side2) :Shape(n){ ... }
The problem is the Shape()
gets executed before the n
is initialized. See constructors-called-before-initializing-variables for details.
To solution to this would be to create a the following methods in the Shape
class
protected:
void InitializeBuffer(const int n)
{
sides = new double[n]
}
and instead of initialize the sides
in the parent constructor, call this InitializeBuffer
with in the constructor of the Shape2D
class
Shape2D(const double side1, const double side2) :Shape()
{
InitializeBuffer (n);
sides[0] = side1; sides[1] = side2;
}
Upvotes: 1