Important: Please read the Qt Code of Conduct -

Undefined referance to class::function

  • Hi everyone,
    I made a gui application. Normally, it works fine. However, I've got error that I mentioned in the title, when I use additional widgets.

    What I mean that I say additional widgets is :

    I have installed a framework that uses the Qt Designer which is called as caQtDM Framework.
    In normal way, it is built by ownway. I mean, there is a script that opens Qt Designer, and makes ui files. There are some additional properties in Property Editor. After creating ui files with that script, there is another script which opens the created ui files.

    P.S : The widgets can't be used by openning Qt Designer without the script that I mentioned.

    My aim is that by using these widgets, I want to make an example. However, I want to use the following property in order to create some algorithms for several purposes :

    Go to slot...

    What I have done is :

    • I created a Qt Widget Application in normal way.
    • I used the framework's script to open Qt Designer and use the widgets on the .ui file that I created.
    • I included the paths of the header files for the widgets that I used. I did this by adding this command into the .pro file :
    INCLUDEPATH += /../../../destination_for_header_files

    However, when I debug the project I've get this error on the "ui_mainwindow.h" file :

    undefined referance to 'caLabel::caLabel(QWidget*)'
    collect2: error: ld returned 1 exit status

    The code is below :

    #ifndef UI_MAINWINDOW_H
    #define UI_MAINWINDOW_H
    #include <QtCore/QVariant>
    #include <QtWidgets/QAction>
    #include <QtWidgets/QApplication>
    #include <QtWidgets/QButtonGroup>
    #include <QtWidgets/QHeaderView>
    #include <QtWidgets/QMainWindow>
    #include <QtWidgets/QMenuBar>
    #include <QtWidgets/QStatusBar>
    #include <QtWidgets/QToolBar>
    #include <QtWidgets/QWidget>
    #include "calabel.h"
    class Ui_MainWindow
        QWidget *centralWidget;
        caLabel *calabel;
        QMenuBar *menuBar;
        QToolBar *mainToolBar;
        QStatusBar *statusBar;
        void setupUi(QMainWindow *MainWindow)
            if (MainWindow->objectName().isEmpty())
            MainWindow->resize(400, 300);
            centralWidget = new QWidget(MainWindow);
    43      calabel = new caLabel(centralWidget);  // Problem is in here ****
            calabel->setGeometry(QRect(90, 40, 90, 28));
            menuBar = new QMenuBar(MainWindow);
            menuBar->setGeometry(QRect(0, 0, 400, 24));
            mainToolBar = new QToolBar(MainWindow);
            MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
            statusBar = new QStatusBar(MainWindow);
        } // setupUi

    I mentioned the problem by adding the line number in front of the line and expressed as there is problem.

    I thought, it may be caused due to finding header files directory. So, I also included the header file as "Add existing file" option, but the error still exists.

    If there is any suggestion, I'll be glad to know that.

    Thanks in advance..

  • Moderators

    @fortyff said in Undefined referance to class::function:

      calabel = new caLabel(centralWidget);  // Problem is in here ****

    You have declared a constructor for the class that accepts a widget as an argument caLabel::caLabel(QWidget*), but you haven't defined such in the .cpp file. Do so and rebuild and the error will disappear.

  • @kshegunov Thanks for the reply. I did as you said. I included the source files of that widget which is "calabel.cpp". However, I now have these error in the source file that I just included :

    undefined reference to `vtable for caLabel'   line 5  /from calabel.h file
    undefined reference to `vtable for caLabel'   line 5  /from calabel.h file
    undefined reference to `vtable for caLabel'   line 5  /from calabel.h file
    undefined reference to `vtable for ESimpleLabel'   line 30 /from esimplelabel.h file
    undefined reference to `vtable for ESimpleLabel'   line 30 /from esimplelabel.h file
    undefined reference to `vtable for ESimpleLabel'   line 30 /from esimplelabel.h file
    collect2: error: ld returned 1 exit status

    There are the same errors occured in the "esimplelabel.cpp" file, but I didn't write down here, since it'll consume too much place.

    The source file and the lines where the errors occur are below :

    #include "calabel.h"
    #include "alarmdefs.h"
    #include <QEvent>
    5  caLabel::caLabel(QWidget *parent) : ESimpleLabel(parent)  // Problem is in here
        thisBackColor = Qt::gray;
        thisForeColor = Qt::black;
        thisBackColorOld = Qt::black;
        thisForeColorOld = Qt::black;
        oldColorMode =Static;
        thisVisibility = StaticV;
    void caLabel::setColors(QColor bg, QColor fg)
        if((bg != thisBackColorOld) || (fg != thisForeColorOld)) {
            thisStyle = "background-color: rgba(%1, %2, %3, %4); color: rgba(%5, %6, %7, %8);";
            thisStyle = thisStyle.arg(
            // oups, was forgotten
            thisBackColorOld = bg;
            thisForeColorOld = fg;
        if(thisStyle != oldStyle || thisColorMode != oldColorMode) {
            oldStyle = thisStyle;
    void caLabel::setBackground(QColor c)
        thisBackColor = c;
        setColors(thisBackColor, thisForeColor);
    void caLabel::setForeground(QColor c)
        thisForeColor = c;
        setColors(thisBackColor, thisForeColor);
    void caLabel::setAlarmColors(short status)
        QColor c;
        switch (status) {
        case NO_ALARM:
        case MINOR_ALARM:
        case MAJOR_ALARM:
        case INVALID_ALARM:
        case NOTCONNECTED:
        setColors(thisBackColor, c);
    esimplelabel.h file is below :
    #ifndef ESIMPLELABEL_H
    #define ESIMPLELABEL_H
    #include <QLabel>
    #include <QStyleOptionFrame>
    #include <QStyle>
    #include <QtDebug>
    #include <qtcontrols_global.h>
    #include "fontscalingwidget.h"
    /** \brief A QLabel that provides the font scaling feature.
     * This class is a QLabel that scales its fonts when resized.
     * This is a base font scaling class for all the objects that need to implement the 
     * font scaling features, such as ELabel.
     * You might have a look at those classes.
     * If your label is <em>left</em> or <em>right</em> aligned, then consider increasing the
     * lateralBorderWidth if you experience some problems of font scaling: an ESimpleLabel with
     * a thick <em>boxed frame</em> might have its string not perfectly fit the space in some circumstances.
     * <br/>Font scaling <em>labels</em> are tailored for <em>center aligned</em>
     * text only. The ESimpleLabel is constructed with a horizontal centered alignment if scalingMode
     * property is different from ESimpleLabel::None, and a right alignment otherwise.
     * @see ELabel
     * @see EFlag
     * @see FontScalingWidget
    30 class QTCON_EXPORT ESimpleLabel : public QLabel, public FontScalingWidget  // Problem is here ***
      /* scalable fonts */
      Q_PROPERTY(bool fontScaleEnabled READ fontScaleEnabled DESIGNABLE false)
      Q_PROPERTY(double botTopBorderWidth READ botTopBorderWidth WRITE setBotTopBorderWidth DESIGNABLE fontScaleEnabled)
      Q_PROPERTY(double lateralBorderWidth READ lateralBorderWidth WRITE setLateralBorderWidth DESIGNABLE fontScaleEnabled)
      Q_PROPERTY(double fontScaleFactor READ fontScaleFactor WRITE setFontScaleFactor  DESIGNABLE false)
      Q_PROPERTY(ScaleMode fontScaleMode READ fontScaleMode WRITE setFontScaleMode)
            enum ScaleMode { None, Height, WidthAndHeight};
    	ESimpleLabel(QWidget *parent);
    	ESimpleLabel(const QString& text, QWidget *parent);
    	virtual void setText(const QString&);
        QString text() const {return QLabel::text(); }
    	QSize calculateTextSpace();	
    55	void setFontScaleMode(ScaleMode m) { FontScalingWidget::setScaleMode((int) m); } // Problem is here   *******
            ScaleMode fontScaleMode() { return (ScaleMode) FontScalingWidget::scaleMode();  }	
    	virtual bool event(QEvent *);
    	virtual QSize sizeHint() const;
    	virtual QSize minimumSizeHint() const;

    I know, it is already consuming too much place, but I thought, it may be helpful to figure the problem out.

    Thanks in advance..

  • Lifetime Qt Champion

    after u added the cpp file, did you run

    also, you could completely clean the build folder to make sure all is re-created.

  • @mrjj Saying that run qmake again , I just run the program, and it runs qmake by itself, as far as I know. Am I right ?

    Now, I just "vtable" error.

    I guess so, it is better to clean or re-built the program again. Thanks for the recommendation.

  • Lifetime Qt Champion


    • I just run the program, and it runs qmake by itself, as far as I know. Am I right ?

    yes but it seems to sometimes cache files/info.
    I often see it when i add the O_OBJECT macro.
    Its not really detected before i run qmake manually.
    Also when editing .pro file it sometimes is not applied 100% automatic.

    So sometimes I must clean folder, run qmake to stop the complaining.
    (on windows. could be local issue for me)

  • @mrjj I understand. I just cleaned the folder and run it again, but the errors still occur.
    I guess, now, I should research what exactly "vtable error" is.

  • Lifetime Qt Champion

    The link i provided does talk about different causes.
    Often its something simple :)

  • @mrjj Thanks a lot then, I'll try to figure it out :)

  • Moderators

    @fortyff said in Undefined referance to class::function:

    There are the same errors occured in the "esimplelabel.cpp" file, but I didn't write down here, since it'll consume too much place.

    Make sure you have all virtual functions that are declared defined. While a declaration of a regular function without a definition is perfectly valid (until it's called), that's not true for virtual methods.