Sign in to follow this  

Abstract classes, inheritance and virtual functions problem with C++

This topic is 3192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I thought I understood the ideas behind inheritance and such like, but some simple code I am trying to compile with VC++ 2005 Express is giving me link errors. The code is as follows:- In one file, ACEntity.h, I have a pure abstract class.
#ifndef ACENTITY_H
#define ACENTITY_H

#include <string>

class ACEntity
{
public:
	std::string sID;

	ACEntity();
	virtual ~ACEntity();

};

#endif
In another, ACSpace.h, I have an abstract class that inherits from ACEntity.
#ifndef ACSPACE_H
#define ACSPACE_H

#include "ACEntity.h"

class ACSpace : public ACEntity
{
public:
	ACSpace();
	virtual ~ACSpace();

	virtual void AcceptEntityEntrance( ACEntity *e)=0;
	virtual void AcceptEntityExit( ACEntity *e)=0;
};

#endif
I then have a concrete class that inherits from ACSpace. The header for this is.
#ifndef CAREA_H
#define CAREA_H

#include "ACSpace.h"

class ACEntity;

class CArea : public ACSpace
{
public:
	float m_flHeight, m_flWidth;

	CArea(float t_flWidth, float t_flHeight); 
	virtual ~CArea();

	void AcceptEntityEntrance( ACEntity *e) {};
	void AcceptEntityExit( ACEntity *e) {};

};

#endif
and the implementation so far is.
#include "CArea.h"

CArea::CArea(float t_flWidth, float t_flHeight)
{
	m_flWidth = t_flWidth;
	m_flHeight = t_flHeight;
}
When I try to compile this, I get the following error.
CArea.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CArea::~CArea(void)" (??1CArea@@UAE@XZ) referenced in function "public: virtual void * __thiscall CArea::`scalar deleting destructor'(unsigned int)" (??_GCArea@@UAEPAXI@Z)
Can someone please explain what I'm doing wrong here, because as far as I can see, what I have should compile? Thanks in advance, Wibbs

Share this post


Link to post
Share on other sites
You simply didn't implement CArea::~CArea()...
You'll probably want to implement ACEntity's constructor and destructor too. Also there's nothing abstract about it, it doesn't have a single pure virtual member?
edit: I shouldn't do random things while typing answers heheh

Share this post


Link to post
Share on other sites
While it is true that your base class destructor should be virtual, your derived classes destructors are then implicitly virtual all the way down, so unless you actually need to do something in a derived class destructor, I wouldn't declare or define one at all. There is no need to declare a derived class destructor just because it is, or to make it, virtual.

Generally, if the base class destructor needs to be declared just to make it virtual rather than to do anything, it is written:


class base
{
public:
virtual ~base(){ }
};

Share this post


Link to post
Share on other sites

This topic is 3192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this