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

Started by
3 comments, last by Wibbs 15 years, 1 month ago
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
Advertisement
It does compile. What it fails to do is link, which is caused by the fact that you are missing an implementation of the CArea destructor.
Mike Popoloski | Journal | SlimDX
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
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(){ }};
Thanks for all the quick responses, problem solved :)

This topic is closed to new replies.

Advertisement