I have a class Base
with a pure virtual function f()
. Another class Derived
derives from Base
. I call f()
from within Derived
. And using g++, I get an error from the linker.
[agnel@dooku tmp]$ g++ pure_virtual_function_call.cpp
/tmp/ccGQLHi4.o: In function `Derived::f()':
pure_virtual_function_call.cpp:(.text._ZN7Derived1fEv[_ZN7Derived1fEv]+0x14): undefined reference to `VirtualBase::f()'
collect2: error: ld returned 1 exit status
It seems to me that the error was caught by the linker. Why didn't the compiler report this error? Why leave it to the linker?
Here is the code:
#include <iostream>
using namespace std;
class VirtualBase {
public:
virtual void f() = 0;
};
class Derived : public VirtualBase {
public:
void f(){
VirtualBase::f();
cout << "Derived\n" ;
}
};
int main(){
Derived d;
d.f();
return 0;
}