DEV Community

sydney
sydney

Posted on

cplusplus code example won't compile

#include <iostream>
class base_class {
public:
virtual void vf1() = 0; //pure virtual
virtual void vf2();
virtual void vf3();
};
class derived_class : public base_class {
public:
void vf1(); // OK if it is virtual also
// virtual void vf1();
// virtual void vf2(int);
virtual void vf3();
};
int main(int argc, char** argv) {
//1 derived_class d;
//2 base_class *bp = &d;
//3 bp->vf1();
//4 bp->vf2();
//5 d.vf2();
}
ERROR MSG: undefined reference to
vtable for derived_class'

This code example, from cplusplus.com, does not compile when any of lines 1-5 are put in! It does when they are commented out, so the classes are OK. The error, line 1 in, points to the makefile line where the .exe file is created from the .o file! The .o file is in the project directory so that is not the problem. Creating the .exe file is. The above error msg is what I get when I compile with line 1 installed! Help!
syd / wt1v
`

Top comments (1)

Collapse
 
syd_71 profile image
sydney

I did find he problem after much fooling around! The solutions that code posted on pretty reliable sites may have problems! In the above code the base class is abstract so method signatures are OK. But the derived class has to have functions defined even is there is an empty body! At the end of all the function declarations put in an empty body by {} and then the compiler will be happy!!