A missing vtable usually means the first non-inline virtual member function has no definition

34,237

Solution 1

Parent::~Parent() is not defined.

You can put the definition directly into the class definition:

class Parent {
   public :
     virtual int func () = 0;
     virtual ~Parent() {};
 };

Or define it separately. Or, since C++11, write virtual ~Parent() = default;.

In any case, a destructor needs a definition.

Solution 2

To help anyone else that comes this way looking for help with "NOTE: a missing vtable usually means the first non-inline virtual member function has no definition."

In my case the error was caused by a missing = 0; at the end of the virtual definition. Make sure all the appropriate virtual definitions have = 0; at the end.

virtual HRESULT function(int testInput) = 0;

Hopefully this saves someone some time.

Solution 3

It should be worth adding that having "no definition" also applies to empty functions.

I have many different subclasses that implement a pure virtual function in each. While I was going through these files to implement the function, I declared the function in the .h file and defined the function in the .cpp file. My plan was just to leave each function body blank for now and implement it later as I have many implementations to write.

However, this will not work. Depending on the compiler you're using, functions that have no body are removed in an attempt to optimize code:

void foo() // removed 
{
}

Because foo is removed, it is as if you never defined it and only declared it, hence "...virtual member function has no definition."

Share:
34,237

Related videos on Youtube

Aparna Chaganti
Author by

Aparna Chaganti

Updated on July 09, 2022

Comments

  • Aparna Chaganti
    Aparna Chaganti almost 2 years

    I am pretty sure this question is duplicate, but my code is different here, the following is my code. It fails with a "Undefined symbols" error, not sure whats missing.

    class Parent {
       public :
         virtual int func () = 0;
         virtual ~Parent();
    
     };
    
    
     class Child : public Parent {
         public :
    
         int data;
         Child (int k) {
            data = k;
          }
        int func() {   // virtual function
           cout<<"Returning square of 10\n";
            return 10*10;
        }
    
        void Display () {
        cout<<data<<"\n";
    
     }
    
     ~ Child() {
    
        cout<<"Overridden Parents Destructor \n";
    
     }
    };
    
    
    
    int main() {
      Child a(10);
     a.Display();
    
     }
    

    The following is the O/P when compiled.

    Undefined symbols for architecture x86_64:
      "Parent::~Parent()", referenced from:
          Child::~Child() in inher-4b1311.o
      "typeinfo for Parent", referenced from:
          typeinfo for Child in inher-4b1311.o
      "vtable for Parent", referenced from:
          Parent::Parent() in inher-4b1311.o
      NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
    
    • Hector
      Hector almost 9 years
      Try Parent::~Parent() = default;
  • Olshansk
    Olshansk about 4 years
    Can the destructor be avoided here altogether?
  • Xavier
    Xavier over 3 years
    "Make sure all the virtual definitions have = 0; at the end" I am sorry but = 0 is basically saying this virtual function is pure virtual, which will in turn turn the who class into abstract class, which sometimes is not the case we want. There can be classes with non-pure virtual functions. This answer is absolutely wrong
  • Cosworth66
    Cosworth66 over 3 years
    @Xavier. Thanks for your feedback, my answer clearly says "In my case the error was caused by...." It was intended to help anyone else that was facing the same problem as I had. It wasn't intended to redefine the spec, just clear up and help with the ambiguity of the error message. That does not make the answer absolutely wrong especially as you say "sometimes it's not the case" which means other times it's absolutely will be the case and therefor correct for those situations.
  • Mooing Duck
    Mooing Duck over 3 years
    @Olshansk: not if you want to construct it at some point. If it's constructible, it must be destructible. virtual doesn't change that.