Search code examples
c++c++11vectorconstructorvtable

How do I resolve the "undefined reference to `vtable for <<ClassName>>" error?


I am a C++ beginner. I have a singleton 'Manager' class as shown below and I get the following error when I build the project in Eclipse:

../src/Manager.hpp:28: undefined reference to 'vtable for Manager'

[The below code is the minimum (almost) to be run in your dev environment or IDEs if you want to try and reproduce the error.]

Manager.hpp

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <unistd.h>

#include "stream_state.h"


class Manager {
    public:
       static Manager* getInstance();
       std::vector<stream_state> m_stateList;      // Why can I not remove std::?

       virtual ~Manager();      // Virtual destructor

    private:
       Manager(){};                               // ERROR points here - why so?
       Manager(Manager const&){};
       static Manager* pSingleton;
};

Manager.cpp

#include "Manager.hpp"

Manager* Manager::pSingleton = 0;

Manager* Manager::getInstance()
{
    if (pSingleton == NULL){
        pSingleton = new Manager;
    }
    return pSingleton;
}

// Other member function implementations

main.cpp

#include <iostream>
#include <stdlib.h>
#include "Manager.hpp"

int main(int argc, char** argv)
{

    Manager* managerObj;
    managerObj = Manager::getInstance();
    // some other code
    return 0;
}

stream_state.h

struct stream_state
{
   FILE* sp;
   bool locked;
};

What I have tried already (and didn't work):
1. I changed the constructor for Manager class to:
Manager::Manager(){}; Error: extra qualification 'Manager::' on member 'Manager'

2. I removed std:: from the line std::vector<stream_state> m_stateList; vector<stream_state> m_stateList; Error: vector does not name a type

Can someone please explain me the undefined reference to 'vtable for Manager' error (and, if possible but no necessary, the vector does not name a type error)?


Solution

  • I see a declaration for ~Manager() but I don't see a definition. This needs to be in Manager.cpp.

    I believe you are getting this obscure message because the vtable holds the addresses of all virtual functions. You only have one virtual function and you haven't defined it.

    I can't reproduce the vector error. Once I add ~Manager::Manager() { } to Manager.cpp, my version compiles fine.