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

Discussion in 'C++' started by Angus, Jan 7, 2007.

  1. Angus

    Angus Guest

    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?
     
    Angus, Jan 7, 2007
    #1
    1. Advertisements

  2. Angus

    Pete C Guest

    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.
     
    Pete C, Jan 7, 2007
    #2
    1. Advertisements

  3. Angus

    Ron Natalie Guest

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

    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.
     
    Ron Natalie, Jan 7, 2007
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christof Krueger
    Replies:
    1
    Views:
    761
    Christof Krueger
    Dec 19, 2003
  2. Polanski24
    Replies:
    1
    Views:
    732
    Rolf Magnus
    Apr 11, 2005
  3. p|OtrEk

    const type& and type const&

    p|OtrEk, Jul 17, 2005, in forum: C++
    Replies:
    6
    Views:
    615
    Ben Pope
    Jul 17, 2005
  4. Replies:
    2
    Views:
    1,098
  5. Javier
    Replies:
    2
    Views:
    894
    James Kanze
    Sep 4, 2007
  6. paulo
    Replies:
    9
    Views:
    960
    James Kanze
    Mar 6, 2009
  7. juglesh
    Replies:
    5
    Views:
    382
    Thomas 'PointedEars' Lahn
    May 15, 2005
  8. length power
    Replies:
    2
    Views:
    257
    Rustom Mody
    Apr 10, 2014
Loading...