Pure Virtual Destructor Linker Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads
Undefined Reference To Vtable Destructor
with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the undefined reference to base class constructor Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: undefined reference to destructor Sign up Why a pure virtual destructor needs an implementation up vote 3 down vote favorite I know the cases where pure virtual destructors are needed. I also know that If we don't provide an implementation for them it will give
C++ Undefined Reference To Vtable
me a linker error. What I don't understand is why this should be the case in a code fragment as shown below: int main() { Base * p = new Derived; } Here there is no delete, so no call to destructor and so no need for its implementation(assuming it is supposed to behave like other normal functions which are declared but not defined, linker complains only when we call them)...or am I missing something? I need to understand why this should be
Undefined Reference To Vtable For Class
a special case? Edit: based on comments from BoBTFish Here are my Base and Derived classes class Base { public: Base(){} virtual ~Base() = 0; }; class Derived : public Base { }; c++ pure-virtual virtual-destructor share|improve this question edited Jan 14 '14 at 9:07 asked Jan 14 '14 at 9:00 Arun 90321122 2 You don't show how you write Base and Derived, but I guess what is happening is the compiler is generating the destructor for Derived, which will try to call the destructor for Base, which does not exist. –BoBTFish Jan 14 '14 at 9:01 It does compile...only linker complains... try it.. I am using VS2012 and I am pretty confident this shouldn't be compiler dependent...and the errors go away once I give an implementation for the ~Base –Arun Jan 14 '14 at 9:09 add a comment| 3 Answers 3 active oldest votes up vote 4 down vote accepted The compiler tries to build the virtual table given a virtual (pure or not) destructor, and it complains because it can't find the implementation. virtual destructors differ from other virtual functions because they are called when the object is destroyed, regardless of whether it was implemented or not. This requires the compiler to add it to the vf table, even if it's not called explicitly, because the derived class destructor needs it. Pedantically, the standard requires a pure virtual destructor to be implemented. share|improve this answer answered Jan 14 '14 at 9:04
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this c++ virtual destructor site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up C++ - http://stackoverflow.com/questions/21109417/why-a-pure-virtual-destructor-needs-an-implementation virtual destructors and linker errors up vote 4 down vote favorite I got this interface I've written: #ifndef _I_LOG_H #define _I_LOG_H class ILog { public: ILog(); virtual ~ILog(); virtual void LogInfo(const char* msg, ...) = 0; virtual void LogDebug(const char* msg, ...) = 0; virtual void LogWarn(const char* msg, ...) = 0; virtual void LogError(const char* msg, ...) = 0; private: Monkey* monkey; }; http://stackoverflow.com/questions/8609880/c-virtual-destructors-and-linker-errors #endif The methods are pure virtual and therefore must be implemented by deriving classes. If I try to make a class that inherits this interface I get the following linker errors: Undefined reference to ILog::ILog Undefined reference to ILog::~ILog I understand why there is a virtual destructor (to make sure the derived's destructor is called) but I do not understand why I get this linker error. EDIT: Okay, so I need to define the virtual destructor as well. But can I still perform stuff in the definition of the virtual destructor, or will it simply call my derived classes destructor and skip it? Like, will this trigger: virtual ~ILog() { delete monkey; } c++ inheritance linker abstract-class share|improve this question edited Dec 22 '11 at 21:51 asked Dec 22 '11 at 21:23 KaiserJohaan 3,441969140 Of course that will trigger. –Truncheon Dec 22 '11 at 21:59 possible duplicate of What is an undefined reference/unresolved external symbol error and how do I fix it? –Akhil V Suku Aug 12 '15 at 6:42 add a comment| 3 Answers 3 active oldest votes up vote 9 down vote accepted You h
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack http://stackoverflow.com/questions/32722206/why-is-calling-a-pure-virtual-a-linker-error-rather-than-a-compile-error Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community http://www.cplusplus.com/forum/general/12712/ of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why is calling a pure virtual a linker error rather than a compile error? up vote 4 undefined reference down vote favorite 3 It's a bit surprising to me that this program: struct A { virtual void a()=0; }; struct B : public A { void a() {} }; int main() { B b; b.a(); // OK, call B::a() b.A::a(); // linker error? } Gave me this error (gcc 4.4): /tmp/ccfOGuBJ.o: In function `main': test.cc:(.text+0x28): undefined reference to `A::a()' collect2: ld returned 1 exit status (clang 7.0.0) Undefined symbols for undefined reference to architecture x86_64: "A::a()", referenced from: _main in test-440cc5.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) I expected that attempting to call a pure function would give an explicit error, because it was declared as deleted, rather than an implicit error. Is there not a distinction in the standard between "function not in this translation unit," and "function not in any translation unit?" It should be noted that attempting to directly call a pure virtual is not addressed in ยง10.4. c++ language-lawyer share|improve this question asked Sep 22 '15 at 16:35 OmnipotentEntity 8,55933370 add a comment| 2 Answers 2 active oldest votes up vote 10 down vote accepted Pure virtual functions can have a body. Therefore the error is a link error, because you fail to provide the body. share|improve this answer answered Sep 22 '15 at 16:38 Remus Rusanu 207k25269406 Ah, the standard is a bit misleading in this case, because it has a line: virtual void f() = 0 { }; // ill-formed. Which is true, but lead me to believe that a pure virtual could not have an implementation, rather than simply mixing these syntaxes are not supported. Thank you! –OmnipotentEntity Sep 22 '15
external symbol "public: virtual __thiscall Shape::~Shape(void)" (??1Shape@@UAE@XZ) referenced in function __unwindfunclet$??0Circle@@QAE@HPAVOperatingSystem@@@Z$0 1>BridgePattern.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall OperatingSystem::~OperatingSystem(void)" (??1OperatingSystem@@UAE@XZ) referenced in function "public: virtual __thiscall Windows::~Windows(void)" (??1Windows@@UAE@XZ) for the following code. Any clues? 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include