Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. (Newbie) Link error with the most simple class
Forum Updated to NodeBB v4.3 + New Features

(Newbie) Link error with the most simple class

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mbp566
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mbp566
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mbp566
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved