0
class BaseClass {
public:
    BaseClass();
    virtual void vfunc1()= 0;
    virtual void vfunc2();
    virtual void vfunc3();
    virtual void vfunc4();
    virtual ~BaseClass();
private:
    int x;
    int y;
};

class SubClass : public BaseClass {
public:
    SubClass();
    virtual void vfunc1(); 
    virtual void vfunc3();
    virtual void vfunc5();
    ~SubClass();
private:
    int z;
};

int main()
{
    BaseClass *bc = new SubClass();
    return 0;
}

Error is the follow:

 Temp\ccsj6Duj.o:demo.cpp:(.text+0x20): undefined reference to `SubClass::SubClass()' collect2.exe: 
error: ld returned 1 exit status

Please help i'm new c++: The problem resides in inhwritance and vtable : This is a demo for better understanding inheritance

4rt1c0
  • 21
  • 3
  • Wheres your implementations? Since you have a `main` method there im guessing you dont have any which is your problem :p – Object object Mar 19 '20 at 10:35
  • I just want the code to compile but i cannot. Suggestions? Tips? Implementations examples ? . I just want bare minimum code possible for compilation . Thx for help – 4rt1c0 Mar 19 '20 at 10:44
  • Bare minimum possible needs implementation, how does the compiler know what your class or function does if you dont tell it? You've got a linker error there because the compiler cant find the implementation of `Subclass`'s constructor – Object object Mar 19 '20 at 10:45
  • 2
    The code compiles; it fails to **link** because you provided no implementation for the code you declared would be part of your build. Ex:: Where is `SubClass::SubClass()` actually *implemented* ? I can see where it is *declared*, but where is the "there" ? – WhozCraig Mar 19 '20 at 10:48
  • Well you're right it fails to link ... i'm just trying to have an .exe that does nothing with an empty class and another empty inherited class. Seems i have to read more. thx – 4rt1c0 Mar 19 '20 at 11:08
  • @4rt1c0 You didn't implement `SubClass::SubClass()` anywhere in the shown code, but you use it when you call `new SubClass();`, so the compiler doesn't know what you want that constructor to do. – walnut Mar 19 '20 at 11:54
  • yes!!!!!! thank you !!!!!!! Zoe I love you !!!! – 4rt1c0 Mar 19 '20 at 11:55

1 Answers1

1
virtual void vfunc1()= 0;

This code means that BaseClass is an abstract class. When an abstract class is inherited, all pure functions must be implemented, otherwise the inherited class is also an abstract class. (except for pure vitual destructors)

Abstract classes are not allowed to define objects and can only provide interfaces for inherited classes. And I think that's why you had an compile error.

Sairam
  • 375
  • 1
  • 5
  • 28
lizb_6626
  • 71
  • 5
  • even if i remove that line it won't link ... i wish empty implementation ... or bare minimum implementations that does nothing ... thanks ... – 4rt1c0 Mar 19 '20 at 11:34
  • OP is overriding the pure virtual method with `virtual void vfunc1();` in `SubClass`, so `SubClass` will not be abstract. However OP seems to not provide implementations for *any* of the declared functions. – walnut Mar 19 '20 at 11:56