3

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.

NKN
  • 6,482
  • 6
  • 36
  • 55
ChrisU
  • 41
  • 1
  • 1
  • 4
  • 2
    Please show us the relevant code as well. – stefan Apr 13 '14 at 21:38
  • 1
    In case of QT, it often means that you haven't rebuilt everything you needed to rebuild. – Mats Petersson Apr 13 '14 at 21:44
  • This is the .h file: #ifndef SERVICIO_H #define SERVICIO_H #include 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 Apr 13 '14 at 21:49
  • 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 – ChrisU Apr 13 '14 at 22:11

4 Answers4

8

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.

Artfunkel
  • 1,832
  • 17
  • 23
6

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.

Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
6

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

Adriel Jr
  • 2,451
  • 19
  • 25
1

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.

Dan
  • 2,460
  • 2
  • 18
  • 12