QT Creator - LNK2019 error when editing the MainWindow.cpp constructor code



  • Added an InitMain function to a brand new QT Widget -> QT Gui Application so that my code calls the InitMain in the MainWindow's constructor. When compiling I get this error:

    mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall MainWindow::InitMain(void)" (?InitMain@MainWindow@@QAEXXZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)

    Any help on resolving this issue would be much appreciated


  • Moderators

    Welcome to devnet

    Do you have InitMain in the same source file as MainWindow?



  • Yes. Here's the .h and .cpp files for MainWindow (everything's default per the project creator other than the InitMain() function stuff)

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    void InitMain();
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    InitMain();
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void InitMain()
    {
    int i = 5;
    i++;
    }
    @

    (I do realize that at this time InitMain is a pointless function)


  • Moderators

    Please tag your code with '@' as explained "here ":http://developer.qt.nokia.com/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01


  • Moderators

    You have implemented an independent function called InitMain(). You may do this, but you have declared a member function of MainWindow calls.

    You should write:
    @
    void MainWindows :: InitMain()
    {
    int i = 5;
    i++;
    }
    @



  • My appologies on not using the 'at' symbol. Thanks also for the solution. Checking it now. I'm not yet used to using the class definition before methods/functions


Log in to reply
 

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