Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi, I have a very annoying problem in this code
C++
class Drawable
{
public :
    virtual void draw() = 0;
    virtual ~Drawable() = 0;
};

class MyDrawable : public Drawable
{
public :
    virtual void draw();
    MyDrawable();
    virtual ~MyDrawable();
private :
    int * data;
};

MyDrawable::~MyDrawable()
{
    delete data;
}

void MyDrawable::draw()
{

}

MyDrawable::MyDrawable()
{
    data = new int;
}

void del(Drawable* drawable)
{
    delete drawable;
}

int main()
{
    del(new MyDrawable());
}

the error messages are :
line 18 : Undefined reference to Drawable::~Drawable()
line 28 : Undefined reference to Drawable::~Drawable()

Could you help me plz ?

thanks, Samuel.
Posted

1 solution

C++
class Drawable
{
public :
    virtual void draw() = 0;
	virtual ~Drawable() {};
};
 
class MyDrawable : public Drawable
{
public :
    virtual void draw();
    MyDrawable();
    virtual ~MyDrawable();
private :
    int * data;
};
 
MyDrawable::~MyDrawable()
{
    delete data;
}
 
void MyDrawable::draw()
{
 
}
 
MyDrawable::MyDrawable()
{
    data = new int;
}
 
void del(Drawable* drawable)
{
    delete drawable;
}
 
int main()
{
    del(new MyDrawable());
}


Why?
http://stackoverflow.com/questions/270917/why-should-i-declare-a-virtual-destructor-for-an-abstract-class-in-c[^]

http://accu.org/index.php/journals/233[^]

[Edit]
A little more info in response to comments:

http://stackoverflow.com/questions/3336499/virtual-desctructor-on-pure-abstract-base-class[^]

You could also have:
C++
class Drawable
{
public :
    virtual void draw() = 0;
	virtual ~Drawable() = 0;
};

Drawable::~Drawable()
{
}

See here:
https://msdn.microsoft.com/en-us/library/hy3y5wee.aspx[^]

http://eli.thegreenplace.net/2010/11/13/pure-virtual-destructors-in-c[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Jun-15 23:57pm    
Sure, a 5.
—SA
[no name] 16-Jun-15 6:42am    
Thanks SA.
Samuel Shokry 16-Jun-15 17:58pm    
Thanks a lot :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900