C++ - Can't access function in another class



  • Hello,

    I've got a problems accessing a function in another class.
    Help would be appriciated.

    Regards

    Compiler: GNU GCC 5.3.0
    Qt Version: 5.11.2
    System: Windows 10 (10.0)

    Header file for the class to access:-

    class QG_CommandEdit: public QLineEdit {
        Q_OBJECT
    
    public:
        QG_CommandEdit(QWidget* parent=0);
        virtual ~QG_CommandEdit()=default; //line 43 ****
    
        void readCommandFile(const QString& path);
        ... ...
    

    my.cpp file

    QString where = qApp->applicationDirPath().append("/").append(fileName); //this is valid
    
    QG_CommandEdit inst;
    inst.readCommandFile( where ); //line 222 ***
    
    errors:-
    error: undefined reference to `vtable for QG_CommandEdit'                  - qg_commandedit.h #43
    error: undefined reference to `QG_CommandEdit::QG_CommandEdit(QWidget*)'   - my.cpp #222
    


  • @mad-hatter

    Is the associated source file included in the .pro file?



  • @koahnig

    Is the associated source file included in the .pro file?

    Yes.


  • Qt Champions 2018

    @mad-hatter please clean your project (worst case: delete build directory), run qmake and build again.


  • Lifetime Qt Champion

    Hi,

    Usually, vtable related errors comes after you added or remove the Q_OBJECT macro. Either re-run qmake by hand or do as @aha_1980 suggested: nuke the build folder and re-build.



  • Hello,

    Thanks for all your repies.

    Deleted build folder
    Deleted *.pro.usr
    Ran clean
    Ran Qmake
    Ran Rebuild

    Deleted build folder
    Deleted *.pro.usr
    Ran clean
    Ran Qmake
    Ran Build

    same errors,

    Is it something to do with virtual?
    virtual ~QG_CommandEdit()=default;

    Regards


  • Lifetime Qt Champion

    Might be a silly question but did you implement your class constructor ?



  • @SGaist
    A silly answer, I don't know about class constructors in this context.


  • Qt Champions 2018

    @mad-hatter said in C++ - Can't access function in another class:

    I don't know about class constructors in this context

    Then you should learn.
    Do you have this in your cpp file:

    QG_CommandEdit::QG_CommandEdit(QWidget* parent=0)
    {
    }
    


  • Hello,

    Thanks for your response.
    I can't change this file as its part of an open source project.

    @jsulm
    This is in the qg_commandedit.cpp file.

    QG_CommandEdit::QG_CommandEdit(QWidget* parent)
            : QLineEdit(parent)
            , keycode_mode(false)
            , relative_ray("none")
            , calculator_mode(false)
    
    {
        setStyleSheet("selection-color: white; selection-background-color: green;");
        setFrame(false);
        setFocusPolicy(Qt::StrongFocus);
    }
    

    Regards



  • @mad-hatter

    Show the .pro or .pri file where the source "qg_commandedit.cpp" is included.



  • @koahnig

    Pro file

    TEMPLATE = app
    
    DISABLE_POSTSCRIPT = false
    
    #uncomment to enable a Debugging menu entry for basic unit testing
    #DEFINES += LC_DEBUGGING
    
    DEFINES += DWGSUPPORT
    DEFINES -= JWW_WRITE_SUPPORT
    
    LC_VERSION="2.2.0-alpha"
    VERSION=$${LC_VERSION}
    
    # Store intermedia stuff somewhere else
    GENERATED_DIR = ../../generated/librecad
    # Use common project definitions.
    include(../../common.pri)
    include(./boost.pri)
    include(./muparser.pri)
    
    CONFIG += qt \
        warn_on \
        link_prl \
        verbose \
        depend_includepath
    
    QT += widgets printsupport
    CONFIG += c++11
    *-g++ {
        QMAKE_CXXFLAGS += -fext-numeric-literals
    }
    
    GEN_LIB_DIR = ../../generated/lib
    PRE_TARGETDEPS += $$GEN_LIB_DIR/libdxfrw.a \
    		$$GEN_LIB_DIR/libjwwlib.a
    
    DESTDIR = $${INSTALLDIR}
    
    # Make translations at the end of the process
    unix {
        LC_VERSION=$$system([ "$(which git)x" != "x" -a -d ../../.git ] && echo "$(git describe --tags)" || echo "$${LC_VERSION}")
    
        macx {
            TARGET = LibreCAD
            VERSION=$$system(echo "$${LC_VERSION}" | sed -e 's/\-.*//g')
            QMAKE_INFO_PLIST = Info.plist.app
            DEFINES += QC_APPDIR="\"LibreCAD\""
            ICON = ../res/main/librecad.icns
            contains(DISABLE_POSTSCRIPT, false) {
                QMAKE_POST_LINK = /bin/sh $$_PRO_FILE_PWD_/../../scripts/postprocess-osx.sh $$OUT_PWD/$${DESTDIR}/$${TARGET}.app/ $$[QT_INSTALL_BINS];
                QMAKE_POST_LINK += /usr/libexec/PlistBuddy -c \"Set :CFBundleGetInfoString string $${TARGET} $${LC_VERSION}\" $$OUT_PWD/$${DESTDIR}/$${TARGET}.app/Contents/Info.plist;
            }
        }
        else {
            TARGET = librecad
            DEFINES += QC_APPDIR="\"librecad\""
            RC_FILE = ../res/main/librecad.icns
            contains(DISABLE_POSTSCRIPT, false) {
                QMAKE_POST_LINK = cd $$_PRO_FILE_PWD_/../.. && scripts/postprocess-unix.sh
            }
        }
    }
    win32 {
        TARGET = LibreCAD
        DEFINES += QC_APPDIR="\"LibreCAD\""
    
        # add MSYSGIT_DIR = PathToGitBinFolder (without quotes) in custom.pro file, for commit hash in about dialog
        !isEmpty( MSYSGIT_DIR ) {
            LC_VERSION = $$system( \"$$MSYSGIT_DIR/git.exe\" describe --tags || echo "$${LC_VERSION}")
        }
    
        RC_FILE = ../res/main/librecad.rc
        contains(DISABLE_POSTSCRIPT, false) {
            QMAKE_POST_LINK = "$$_PRO_FILE_PWD_/../../scripts/postprocess-win.bat" $$LC_VERSION
        }
    }
    
    DEFINES += LC_VERSION=\"$$LC_VERSION\"
    
    # Additional libraries to load
    LIBS += -L../../generated/lib  \
        -ldxfrw \
        -ljwwlib
    
    INCLUDEPATH += \
    HEADERS += ui/lc_actionfactory.h \
        ....
        ui/qg_commandedit.h \
        ... 
    SOURCES += ui/lc_actionfactory.cpp \
        ...
        ui/qg_commandedit.cpp \
        ...
    

  • Moderators

    @mad-hatter said in C++ - Can't access function in another class:

    INCLUDEPATH +=
    HEADERS += ui/lc_actionfactory.h \

    Add a newline between these lines! Or rather, remove the INCLUDEPATH entirely.



  • @sierdzio

    Thanks for your reply.
    I don't want to alter the pro file as it belongs to an open source project that works as it is.

    Regards


  • Moderators

    It is a bug, at least tell them to fix it if you don't want to do it yourself. If a project is open source, by the way, you do have all the rights to do that modification on your own.



  • Hello,

    @sierdzio

    INCLUDEPATH +=
    HEADERS += ui/lc_actionfactory.h \

    I'm sorry, ' INCLUDEPATH += ' should not be there, its a copy and paste error on my part.

    Regards



  • Hello,

    I've changed the code and I am left with one error now.
    Is there something I should add in the header file?
    Help please!

    Regards

    QG_CommandEdit *inst = nullptr;
    inst->QG_CommandEdit::readCommandFile( where ); //line 248
    
    error: undefined reference to `QG_CommandEdit::readCommandFile(QString const&)' // line 248
    

  • Qt Champions 2018

    Hi @mad-hatter

    Is there something I should add in the header file?

    That would help to resolve compile errors, when the compiler cannot find the definition of a function.

    Linker errors mean, the linker cannot find the implementation of the function.

    Usually this means either the implementation is missing in your source file or you have it in a source file that is not compiled/linked.

    Regards


Log in to reply