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

(Newbie) Link error with the most simple class



  • Hi,

    I'm new to Qt and I use v5.2.
    I made a copy of one of the samples (hellogl, but I don't think this is of importance) to a directory where I'll store all my projects. I opened the copied project, compiled and runned it (in QtCreator). Everything worked fine.

    Then I created the most simple class possible:

    • Right click on project in QtCreator
    • Add new...
    • C++ class
    • I use the following parameters in the wizard: Class name: settings, Base class: QObject, Type information: inherits QObject (automatically set by the wizard), Header file: settings.h (automatically set by the wizard), Source file: settings.cpp (automatically set by the wizard), Path: <my project path, same as .pro> (automatically set by the wizard)
    • I did not add any code to this new class, so basically it only has a constructor:
      @
      #ifndef SETTINGS_H
      #define SETTINGS_H

    #include <QObject>

    class Settings : public QObject
    {
    Q_OBJECT
    public:
    explicit Settings(QObject *parent = 0);

    signals:

    public slots:

    };

    #endif // SETTINGS_H
    @

    The project builds fine with the new class.

    Then I tried to use it (directly in main.cpp, I just added the include and the instantiation on line 5 and 10):
    @
    #include <QApplication>
    #include <QDesktopWidget>

    #include "window.h"
    #include "settings.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    Settings *settings = new Settings();
    Window window;
    window.resize(window.sizeHint());
    int desktopArea = QApplication::desktop()->width() *
    QApplication::desktop()->height();
    int widgetArea = window.width() * window.height();
    if (((float)widgetArea / (float)desktopArea) < 0.75f)
    window.show();
    else
    window.showMaximized();
    return app.exec();
    }
    @

    Now when I build (or rebuild, or clean/build) I get this link error:
    main.obj:-1: error: LNK2001: unresolved external symbol "public: __thiscall Settings::Settings(class QObject *)" (??0Settings@@QAE@PAVQObject@@@Z)

    I tried to search online for solution, but I guess it is such a basic thing that nothing was available...Anyway please tell me if you have any ideas.

    Thank you in advance.


  • Qt Champions 2017

    Welcome to the Qtworld.
    Change you line# to Settings *settings = new Settings;



  • Thanks for the reply, but it doesn't change anything, I still get the same link error.



  • Ok, I did make it work, however I'm not sure why.

    Anyway, I was building with the MSVC2010 OpenGL toolkit. I thought I'd give a try to mingw...
    So I installed the kit with the Qt maintenance program, changed the current kit and it worked ! I could build and all...

    Now the very troubling thing is that when I re-selecting the MSVC kit, then I could build and everything worked with that kit too.

    I didn't change any line of my code, so my best guest is that the maintenance program managed somehow to make it work...

    Anyway, problem solved.


Log in to reply