Jump to content
  • Advertisement
Sign in to follow this  

Library's and pure virtual destructor

This topic is 3113 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

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
Advertisement
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!