Creating object from the child of an abstract class causes to linkage errors

R

romayankin

Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/
protected:
CMyBase();
public:
virtual ~CMyBase()=0;

};

class CMyChild : public CMyBase {
public:
CMyChild();
void close();
~CMyChild();
private:
int temp;
};
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include "main.h"

CMyChild::CMyChild()
: temp (0)
{

}

CMyChild::~CMyChild() {
close();
}

void CMyChild::close() throw() {
temp = 100;
}


int main(int argc, char **argv)
{
CMyChild child;
child.close();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I get to link errors:

main.obj : error LNK2019: unresolved external symbol "protected:
__thiscall CMyBase::CMyBase(void)" (??0CMyBase@@IAE@XZ) referenced in
function "public: __thiscall CMyChild::CMyChild(void)"
(??0CMyChild@@QAE@XZ)

main.obj : error LNK2019: unresolved external symbol "public: virtual
__thiscall CMyBase::~CMyBase(void)" (??1CMyBase@@UAE@XZ) referenced in
function "public: virtual __thiscall CMyChild::~CMyChild(void)"
(??1CMyChild@@UAE@XZ)
 
T

Tomás

posted:
Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/

Should that not return a reference?
protected:
CMyBase();

No point putting that there. Anyway it should be:

CMyBase() {}
public:
virtual ~CMyBase()=0;


Not sure if you can have a _pure_ virtual constructor.


-Tomás
 
B

Ben Pope

Can someone explain what am I doing wrong?

Header:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class CMyBase {
private:
CMyBase(const CMyBase& obj); /* Disable copy constuctor */
void operator=(const CMyBase& obj); /* Disable assignment operator
*/

Usually the assignent op looks like:
T& operator=(const T& rhs)
protected:
CMyBase();
public:
virtual ~CMyBase()=0;

};

The base constructor and destructor will be called before and after the
derived constructor and destructor, respectively, they must be implemented.

Ben Pope
 
R

romayankin

Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor? That's strange, however it
seems there is no other way to avoid handmade implementation
 
P

Pete Becker

Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor? That's strange, however it
seems there is no other way to avoid handmade implementation

An abstract class is a class that has one or more pure virtual
functions. That doesn't mean that it can't do anything at all.

class shape
{
public:
shape(int x0, int y0) : x(x0), y(y0) {}
void draw()
{
do_draw(x, y);
}
virtual ~shape() {}
private:
virtual void do_draw(int, int) = 0;
int x, y;
};

This particular one doesn't do anything interesting in its destructor,
but its constructor is essential.
 
G

Gavin Deane

Thank you, that was actually the original code that I once had. However
I thought that since the base class is abstract why would I have to
implement its constructor and destructor?

So you can create and destroy objects of that type. You declared the
constructor and destructor and the compiler needs to use them. You must
have a concrete type that derives from the abstract type, right? When
you create and destroy an object of that concrete type, the base object
of the abstract type is created and destroyed as part of the process.

If a class is abstract, that doesn't mean that no objects of that type
can ever be created. You can create as many objects of abstract type as
you wish. But *only* as base objects within objects of a non-abstract
derived type.

Gavin Deane
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
BrentonMcc
Top