undefined reference to `vtable for '



  • Linker Error

g++ -pedantic -Wall -std=c++11 -c skip_list.h skip_list.cpp
g++ -pedantic -Wall -std=c++11 skip_list.o -o skip_list
skip_list.o: In function `SkipList::SkipList()':
skip_list.cpp:(.text+0x18): undefined reference to `vtable for SkipList'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

  • Solution
This occurs because the Derived Class did not implement all the functions from the Parent Class, which consist of all pure virtual function.

eg. List.h
     
      class List{
              public:
                               virtual ~List(){};
                               virtual Node *Search(unsigned int key);
      };

      class SkipList : public List{
              public:
                               ~SkipList();
                               Node* Search(unsigned int key){
                               }
       };

The virtual destructor ensure that the SkipList pointed by the List pointer will be correctly deleted. A common mistake is to miss out implementing the virtual destructor and it would result in the above error.

Simply implement it as follows:

  class SkipList : public List{
              public:
                               ~SkipList(){};
                               Node* Search(unsigned int key){
                               }
       };

Popular Posts