Library's and pure virtual destructor

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

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 on other sites
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 on other sites
Quote:
 Original post by ConcentrateUsually 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 compilercreates 1 for that class. But what you have done is that you told the compilerthat you will supply the destructor by creating its prototype, but since its neverdefined, then that means the compiler can't find a definition of a destructor forthat 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 on other sites
You also need to provide a body for your destructor even if it's pure virtual.

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!

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 14
• 13
• 14
• 73
• 22
• Forum Statistics

• Total Topics
632143
• Total Posts
3004331

×