Sign in to follow this  
Hurp

Library's and pure virtual destructor

Recommended Posts

Hurp    106
I have a library which contains a class, then another library that will extend that class. However, the class in the first library contains a pure virtual destructor. This is causing a linking error on my application because it can not fine the destructor, even though it is pure virtual so it should not be looking for it. Does anyone know why or how to fix this? 1>Libext.lib(libextmain.obj) : error LNK2019: unresolved external symbol "public: virtual __thiscall CLibclass::~CLibclass(void)" (??1CLibclass@@UAE@XZ) referenced in function "public: virtual __thiscall CLibextclass::~CLibextclass(void)" (??1CLibextclass@@UAE@XZ) Original -> Lib.h
#pragma once

class CLibclass
{
public:
	CLibclass();
	virtual ~CLibclass() = 0;

	void testfunc();
};

Original -> Lib.cpp
#include "libmain.h"

CLibclass::CLibclass()
{

}

void CLibclass::testfunc()
{

}

Extended -> Lib.h
#pragma once
#include "..\Lib\Libmain.h"

class CLibextclass : public CLibclass
{
public:
	CLibextclass();
	~CLibextclass();

	void testfunc();
};

Extended -> Lib.cpp
#include "libextmain.h"

CLibextclass::CLibextclass()
{

}
CLibextclass::~CLibextclass()
{

}

void CLibextclass::testfunc()
{

}

main.cpp
#include "libextmain.h"

#pragma comment(lib,"Lib.lib")
#pragma comment(lib,"Libext.lib")

int main(void)
{
	CLibextclass test;
	test.testfunc();

	return(0);
}

Share this post


Link to post
Share on other sites
Concentrate    181
Usually virtual function does not have to have its body defined, but in the
special case of a virtual destructor, you need to define its body.

When a class does not have its constructors or destructor defined, the compiler
creates 1 for that class. But what you have done is that you told the compiler
that you will supply the destructor by creating its prototype, but since its never
defined, then that means the compiler can't find a definition of a destructor for
that class. Thus you get a linking error.

Share this post


Link to post
Share on other sites
Hurp    106
Quote:
Original post by Concentrate
Usually virtual function does not have to have its body defined, but in the
special case of a virtual destructor, you need to define its body.

When a class does not have its constructors or destructor defined, the compiler
creates 1 for that class. But what you have done is that you told the compiler
that you will supply the destructor by creating its prototype, but since its never
defined, then that means the compiler can't find a definition of a destructor for
that class. Thus you get a linking error.


I know that if it is just a virtual destructor that I must provide a body, however, I want this to be a pure virtual destructor.

Share this post


Link to post
Share on other sites
iMalc    2466
Any function defined as pure virtual can also have the function body defined, and in this special case it is mandatory.
Not only that, but they can be called too!

Share this post


Link to post
Share on other sites

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