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. Advertising

  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. Advertising

  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. Advertising

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:
    612
    Christof Krueger
    Dec 19, 2003
  2. juglesh
    Replies:
    5
    Views:
    223
    Thomas 'PointedEars' Lahn
    May 15, 2005
  3. length power
    Replies:
    2
    Views:
    109
    Rustom Mody
    Apr 10, 2014
  4. Skip Montanaro
    Replies:
    0
    Views:
    73
    Skip Montanaro
    Apr 10, 2014
  5. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    63
    Johannes Schneider
    Apr 10, 2014
Loading...

Share This Page