Undefined symbols for architecture x86_64 vtable

10,839

Solution 1

The linker prints those errors because definitions of your functions don't exist. You declared 3 functions but you defined only the constructor.

Erasing the virtuals helps because then the linker don't need these functions. With virtual, the linker uses them to create a vtable.

The error will come back if you use these functions in any other place in the program without defining them.

Solution 2

PSA since this is a top Google search result: if you are using Qt, this error can also mean that you didn't build the MOC object file for that class.

Solution 3

If you are using QT, in the vast majority of cases you get the vtable error because you didn't re-run qmake after adding the Q_OBJECT macro to a file.

Credits to: https://forum.qt.io/post/178663

Solution 4

NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

So where is the definition for the two functions you have declared?

virtual string toString() const;
virtual ~servicio();

They dont exist. The compiler told you they dont exist. And No I'm taking the time to tell you that the compiler told you they dont exist, and that they dont exist.

So thats why the compiler tells you that they dont exist. Becuase they dont exist.

Share:
10,839
ChrisU
Author by

ChrisU

Updated on June 04, 2022

Comments

  • ChrisU
    ChrisU almost 2 years

    I'm trying to do a project in Netbeans C++ for mac, when I do a simple constructor for a class named servicio the compiler shows me the following error:

    "/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
    "/Applications/Xcode.app/Contents/Developer/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/medicos
    mkdir -p dist/Debug/GNU-MacOSX
    g++     -o dist/Debug/GNU-MacOSX/medicos build/Debug/GNU-MacOSX/agenda.o build/Debug/GNU-MacOSX/cita.o build/Debug/GNU-MacOSX/contenedor.o build/Debug/GNU-MacOSX/doctor.o build/Debug/GNU-MacOSX/fecha.o build/Debug/GNU-MacOSX/main.o build/Debug/GNU-MacOSX/objetoBase.o build/Debug/GNU-MacOSX/paciente.o build/Debug/GNU-MacOSX/padecimiento.o build/Debug/GNU-MacOSX/servicio.o 
    Undefined symbols for architecture x86_64:
      "vtable for servicio", referenced from:
          servicio::servicio(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in servicio.o
      NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[2]: *** [dist/Debug/GNU-MacOSX/medicos] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    

    I'm not really sure what the problem might be, I'm a beginner at programming so I do not know much about this, Thanks for helping me.

    • stefan
      stefan about 10 years
      Please show us the relevant code as well.
    • Mats Petersson
      Mats Petersson about 10 years
      In case of QT, it often means that you haven't rebuilt everything you needed to rebuild.
    • ChrisU
      ChrisU about 10 years
      This is the .h file: #ifndef SERVICIO_H #define SERVICIO_H #include <string> using namespace std; class servicio { public: servicio(string); virtual string toString() const; virtual ~servicio(); private: string nombre; }; #endif /* SERVICIO_H */ and this is the .cpp: #include "servicio.h" servicio::servicio(string nombre):nombre(nombre){ } just trying to do that simple constructor
    • ChrisU
      ChrisU about 10 years
      I errase the virtuals and the const and now it is running, I don't know why but it works, if someone knows why please tell me, Thanks
  • Evan Murray
    Evan Murray about 4 years
    Thank you so much! I am new to C++ as well, and this helped for me!
  • Zachary HUANG
    Zachary HUANG over 3 years
    I just want to say thank you!!!!! I've been struggling with this issue the whole afternoon and you save my time!