Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

use of STL causing LINK2019 linker error



  • Hello, So I have the following function:

    void StartWindow::newPOTWButtonClicked()
    {
        std::cout << "creating new POTW" << std::endl;
    
        //generate new window
        POTWSetupWindow setupWindow;
    
        //generate questions
        QuestionBox* week = new QuestionBox("What week is it?");
    
        //add question elements and link to correct slot
        std::function<void(void)>func(std::bind(&POTWSetupWindow::weekFilled, &setupWindow));
        week->addDropDownItem(1,100, func);
        
        //push questions to POTW setup window
        setupWindow.addQuestion(week);
    
        emit setNewMainWindow(setupWindow);
    }
    
    

    However, this throws the following linker error:

    startwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl POTWSetupWindow::weekFilled(void)" (?weekFilled@POTWSetupWindow@@QEAAXXZ) referenced in function "public: void __cdecl StartWindow::newPOTWButtonClicked(void)" (?newPOTWButtonClicked@StartWindow@@QEAAXXZ)
    

    Now, the error goes away If I run the code without the use of std::function and std::bind , so it appears to me that they are causing the problems. My .pro file looks as follows, which likely needs to be changed to solve my problem:

    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    CONFIG += c++14
    
    DEFINES += QT_DEPRECATED_WARNINGS
    
    SOURCES += \
        button.cpp \
        fancyslider.cpp \
        main.cpp \
        mainwindow.cpp \
        potwsetupwindow.cpp \
        startwindow.cpp \
        questionbox.cpp \
    
    HEADERS += \
        button.h \
        fancyslider.h \
        mainwindow.h \
        potwsetupwindow.h \
        startwindow.h \
        questionbox.h \
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    Also, my project is using Desktop Qt 5.14.2 MSCV2017 64 Bit



  • @jonah765 said in use of STL causing LINK2019 linker error:

    std::function<void(void)>func(std::bind(&POTWSetupWindow::weekFilled, &setupWindow));

    Why are you using std::function and std::bind? std::bind should return a function object.

    You are also storing a pointer to an object that will be destroyed at the end of this function:

    POTWSetupWindow setupWindow;
    

    Is your setupWindow QObject based? Then you can create it with new and have the parent object in the constructor. This will handle destruction when the parent window/object gets deleted.

    Edit:
    How is weekFilled defined?



  • Thanks a lot for your response, I appreciate the effort, but the error was in the end something trivial.


  • Lifetime Qt Champion

    @jonah765 said in use of STL causing LINK2019 linker error:

    Thanks a lot for your response, I appreciate the effort, but the error was in the end something trivial.

    ... and would you like to share the trivial solution? It might help later readers.

    Thanks


Log in to reply