# Can't compile Qt code (again)



## caesius (Jan 9, 2010)

I'm up to this chapter of the official Qt tutorial. I've copied and pasted the three source files and I'm trying to compile them but I get these messages:


```
> g++ -I/usr/local/include/qt4/QtGui -I/usr/local/include/qt4 -I/usr/local/include -L/usr/local/lib/qt4 -lQtGui -lQtCore main.cpp lcdrange.cpp
/var/tmp//ccP3Tdr9.o(.text+0x194): In function `LCDRange::LCDRange(QWidget*)':
: undefined reference to `vtable for LCDRange'
/var/tmp//ccP3Tdr9.o(.text+0x19e): In function `LCDRange::LCDRange(QWidget*)':
: undefined reference to `vtable for LCDRange'
/var/tmp//ccP3Tdr9.o(.text+0x434): In function `LCDRange::LCDRange(QWidget*)':
: undefined reference to `vtable for LCDRange'
/var/tmp//ccP3Tdr9.o(.text+0x43e): In function `LCDRange::LCDRange(QWidget*)':
: undefined reference to `vtable for LCDRange'
>
```

Can anyone who knows about Qt shed some light on what's happening?

Thanks.


----------



## expl (Jan 9, 2010)

I am not a Qt programmer but I think you need to use Qt Meta Object Compiler because you have Q_OBJECT macro in your class.

http://qt.nokia.com/doc/4.6/moc.html


----------



## caesius (Jan 9, 2010)

Thanks expl, but I don't think that's the problem because I get the same error when using qmake.

I see now that the code *compiles* fine, it's the linking that is throwing the errors. I've googled _undefined reference to vtable_ and it seems this is a common error (see here, here, here and here), but I don't know enough C++ to understand the explanations. 

All of the threads on other forums seem to point to the same FAQ page, http://gcc.gnu.org/faq.html#vtables, but again, I don't understand it.

I gathered it's something about class definitions not being in header files. Any C++ experts care to voice their opinions?

Thanks


----------



## expl (Jan 9, 2010)

There is nothing complicated like I said you need to use Qt4 moc, it will generate the needed stuff then you just compile it and link it with rest of your project. Like this:


```
moc-qt4 -o lcdrange-moc.cpp lcdrange.h
g++ -I/usr/local/include/qt4/QtGui -I/usr/local/include/qt4 -I/usr/local/include -L/usr/local/lib/qt4 -lQtGui -lQtCore main.cpp lcdrange.cpp lcdrange-moc.cpp
```


----------



## caesius (Jan 11, 2010)

Thanks expl, you solved it.


----------

