Undefined reference to 'vtable for classname

Source: Internet
Author: User

This error message is too common. If you have used QT for more than two months, you can solve it yourself. It is too classic and can be used for reference by new users.

The error message is generally similar to: Undefined reference to 'vtable for classname mycompon'
It appears after executing the make command.

The error is caused by the addition of q_object after the class definition in order to use signals and slot when defining the class. Because q_object is a macro, different code is displayed in different classes, such as

public:template  inline void qt_check_for_QOBJECT_macro(const T &_q_argument) const {int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i;}static const QMetaObject staticMetaObject;virtual const QMetaObject *metaObject() const;virtual void *qt_metacast(const char *);static inline QString tr(const char *s, const char *c = 0) {    return staticMetaObject.tr(s, c);}static inline QString trUtf8(const char *s, const char *c = 0){    return staticMetaObject.trUtf8(s, c);}static inline QString tr(const char *s, const char *c, int n) { return staticMetaObject.tr(s, c, n);}static inline QString trUtf8(const char *s, const char *c, int n) {return staticMetaObject.trUtf8(s, c, n);}virtual int qt_metacall(QMetaObject::Call, int, void **); private:

The code above declares three virtual functions inherited from the parent class. Originally, the implementation of these three functions should be completed by moc_mywidget.cpp.
The current error is caused by the fact that the makefile does not add moc_mywidget.cpp to the compilation.

The reason behind makefile is qmake, because the QT compilation system converts the. Pro file to the MAKEFILE file through qmake. When qmake scans. H code and finds code with the words q_object, it writes a dependency between moc_xxx.cpp code generated by MOC to makefile. If no q_object is found during the scan, no additional files such as moc_xxx.cpp will be generated.

The reason for the first scene is that when qmake is executed, There is no code like o_object in the. H code. When you execute make, q_object already exists in. h. The solution is to re-Execute qmake and then execute make.

--

Shiroki: Another possibility is that the code writer writes all the code in the. cpp file. You need to know that the MOC tool is read-only. h file. If you put the q_object macro in CPP, MoC will not be able to see it ...... Therefore, it is better for everyone to write code by following the rules of declaring put. h to implement put CPP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.