469,917 Members | 1,700 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

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

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?
Jan 7 '07 #1
2 6917
Angus wrote:
>
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.

Jan 7 '07 #2
Pete C wrote:
Angus wrote:
>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.
It's a defect in some versions of VC++. They won't delete const
objects. Try a const_cast to remove the const.
Jan 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

67 posts views Thread by Steven T. Hatton | last post: by
6 posts views Thread by Peter Frost | last post: by
2 posts views Thread by Hendrik Schober | last post: by
3 posts views Thread by Adriano | last post: by
2 posts views Thread by adsci | last post: by
Schwack
2 posts views Thread by Schwack | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.