2

To begin, I know that there are a lot of questions about this but I didn't find a solution for this, so I decided to write it.

I'm working on a C++ project and I need to use polymorphism. In order to solve my problem, I simplified it into two classes: Parent and Child. The Parent has only virtual methods, and the Child has to implement them. The only class that will be instantiated is the Child. So, here is the code:

Parent.hh

namespace ParentNamespace {
class Parent {
    public:
        Parent();
        ~Parent();
        virtual int Start() = 0;
    };
}

Parent.cc

#include "Parent.hh"
using namespace ParentNamespace;
namespace ParentNamespace { 
    Parent::Parent(){}
    Parent::~Parent(){} 
}

Child.hh

#include "Parent.hh"
namespace ChildNamespace {
    class Child : public ParentNamespace::Parent {
        public:
            Child();
            ~Child();
            int Start();
    };
}

Child.cc

#include "Child.hh"
namespace ChildNamespace {
    Child::Child(){}
    Child::~Child(){}   
    int Start(){
        return 0;
    }
}

It compiles without errors (it produces .o files), but when it has to link them, it shows this error:

In function ChildNamespace::Child::Child(): Child.cc:8: undefined reference to vtable for ChildNamespace::Child

I've tried with the responses from the other questions, but no success. I suppose that I can't see something simple, so please help!

Thanks in advance.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Nomce
  • 738
  • 1
  • 6
  • 18
  • See https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable – Nepho Nov 10 '17 at 14:43
  • @Nepho I have seen it, and dozens similar, it didn't helped... in the end, it shows similar questions when you start to write the title. Thank you anyway. – Nomce Nov 10 '17 at 14:45
  • 1
    Then, can you post the content of your Makefile/the commands you use to compile the files? It shouldn't be hard to see where the mistake is. – Nepho Nov 10 '17 at 14:47
  • 2
    Make the destructor of `Parent` virtual. – Richard Critten Nov 10 '17 at 14:47
  • 4
    Also, it's `int Child::Start()` and not `int Start()`. – Nepho Nov 10 '17 at 14:48
  • @RichardCritten Thank you, that was the problem. It compiles/links now, without problems. Can you formally write the answer so I can accept it? – Nomce Nov 10 '17 at 15:02
  • @RichardCritten Yes, in general, a dtor of a class used as base class should *often* be virtual. Esp. if that class has already virtual function members, which means it pays the cost of vptr already. It isn't an absolute and not the issue here. – curiousguy Nov 17 '17 at 13:38
  • @RichardCritten your solution helps,Thanks! – vivek_v Sep 17 '20 at 06:42

1 Answers1

1

You need to implement the pure virtual function, add Child:: to Start method In Child.cc

 #include "Child.hh"
 namespace ChildNamespace {
     Child::Child(){}
     Child::~Child(){}   
     int Child::Start(){
         return 0;
     }
 }

I hope this help you

Lior
  • 508
  • 3
  • 18