473,394 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 7221
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Hendrik Schober | last post by:
Hi, I have some expression template code that I want to get to work. I had a typo in it and just spent two hours to find it, because VC7.1 gave a really weird error message for it. This is the...
3
by: Adriano | last post by:
Hello, when I try to print something, either DataGrid or from Crystal Report viever the folowing error message appears and cancels printing: Object reference not set to an instance of an...
2
by: Joseph Lu | last post by:
Hi, I have a multithread problem like the following lines, when I compile this code I caught a "error C2665", the error description is : none of the number1 overloads can convert parameter number2...
3
by: John Shell | last post by:
Hello, all. The following code results in a C2666 error (2 overloads have similar conversions). class FSVec2D { public: FSVec2D() { // code omitted }
2
by: adsci | last post by:
Hello! Im posting this to c.l.c++ AND win32 because i dont know if this is a MS Compiler Issue or not. here we go: <code> class MyCString
2
Schwack
by: Schwack | last post by:
I've just started learning C++ (got bored at work) and I'm using VC++ to compile some simple code but I get a compile error in test.cpp when using "getline". I've searched the internet and this...
6
by: Ahmedhussain | last post by:
Hi there, I m doing work on a gridview and Im getting an error: A potentially dangerous Request.Form value was detected from the client (ctl00$Content$GridView1$ctl03$TextBox1="<span...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.