473,382 Members | 1,273 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,382 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 7219
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.