Error: undefined reference to `vtable for MyClass'
-
What am I doing wrong?
@class MyClass : public QObject{
Q_OBJECT
public:
MyClass(const QString& text, QObject* parent = 0)
:m_text(text), QObject(parent){} const QString& text() const{return m_text;} int getLengthOfText() const{return m_text.length();}
public slots:
void setText(const QString& text){m_text = text;}signals:
void textChanged(const QString&);private:
QString m_text;};@
I get the error on the line of the constructor initializer list, line 5
-
Right click on your project and select "Run qmake" to for a new build of MOC classes. It usually does run automatically, but sometimes it "forgets to".
@qxoz - vtable is virtual functions' reference table. "LINK":http://en.wikipedia.org/wiki/Virtual_method_table
-
[quote author="ddriver" date="1323250137"]
[quote author="Andre" date="1323249955"]Did you run moc on it?
[/quote]Doesn't it run automatically on building the project with Qt Creator? If not, what steps do I need to take to run it manually?
[/quote]
Well, you did not show us the .pro file or anything like that, so there was no way for me to know what you use to develop this thing. I would first try the Build > Run qmake command from the menu. If that doesn't work, do a complete rebuild.BTW: you realize that your textChanged() signal will not be emitted by your current code?
-
I clean the project, run qmake manually, rebuild and still get the same error.
Andre - I had the emit in the implementation but removed it to test with simpler inlined definitions.
The problem probably lies in the .pro file - I haven't done anything special to it, just started a new console application. Here is the content Qt Creator generated for me:
@QT += coreQT -= gui
TARGET = untitled
CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp@
Any suggestions?
-
Yes: add the actual header and source files using the HEADERS and SOURCES variables.
Currently, qmake has no idea about your class and that it needs to build it. Note that HEADERS is strictly speaking only needed for objects that need the meta-object framework to do its magic on them (that includes all QObject subclasses), but it is a good habbit to just include all headers there; at least in a pure Qt application.
-
Thanks a lot, I will keep this limitation in mind from now on, I have implemented many classes in the main.cpp before and had no issues, but that was probably because I was doing very basic stuff with standard c++.
The error is gone after I put the class into a separate header file.
-
And if you use cmake, add the following lines to the CMakeList.txt file:
SET(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTOUIC ON)
SET(CMAKE_AUTORCC ON) -
Hi @asheesh,
the correct way is to run
qmake
and then (re)build the project. With removing and adding the files you probably triggered this.QtCreator 4.10 contains a fix to re-run qmake on project rebuild, so this error occur much less nowadays.
Regards