error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from type 'const class xyz*'


A

Angus

Hello

I have some classes defined in shapes.h and use them like this:


const IShape* rectangle = new Rectangle(10.0, 20.0); // width, height

cout << rectangle->calculateArea() << endl;

delete rectangle; // causes error C2665 error


Error I get is:
error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from
type 'const class IShape *'

My shapes.h
class IShape
{
public:
virtual double calculateArea() const = 0; // returns area
};

class Rectangle : public IShape
{
public:
Rectangle(double width, double height)
{
m_width = width;
m_height = height;
}
double calculateArea() const
{
return m_width * m_height;
}
double m_width; // width
double m_height; // height
};

class Circle : public IShape
{
public:
Circle(double radius)
{
m_radius = radius;
}
double calculateArea() const
{
return 2 * PI * m_radius;
}
double m_radius; // radius of circle
static const double PI = 3.14;
};

How do I fix this?
 
Ad

Advertisements

P

Pete C

Angus said:
const IShape* rectangle = new Rectangle(10.0, 20.0); // width, height
cout << rectangle->calculateArea() << endl;
delete rectangle; // causes error C2665 error

Error I get is:
error C2665: 'delete' : none of the 2 overloads can convert parameter 1 from
type 'const class IShape *'

My shapes.h
class IShape
{
public:
virtual double calculateArea() const = 0; // returns area
};

You definitely want a virtual destructor in here if you are deleting
via pointers to IShape:
virtual ~IShape() {}
That said, I can't see why you'd get a compilation error. g++ compiles
it without complaint.
 
Ad

Advertisements

R

Ron Natalie

Pete said:
You definitely want a virtual destructor in here if you are deleting
via pointers to IShape:
virtual ~IShape() {}
That said, I can't see why you'd get a compilation error. g++ compiles
it without complaint.
It's a defect in some versions of VC++. They won't delete const
objects. Try a const_cast to remove the const.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top