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



  • Did you run moc on it?



  • [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?



  • What is vtable?
    Or maybe you didn't post all code of your class.

    I create new class and paste yours class as is and there are no errors.


  • Moderators

    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 += core

    QT -= gui

    TARGET = untitled
    CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = 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.


  • Banned

    This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.