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
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:
class Settings : public QObject
explicit Settings(QObject *parent = 0);
#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):
int main(int argc, char *argv)
QApplication app(argc, argv);
Settings *settings = new Settings();
int desktopArea = QApplication::desktop()->width() *
int widgetArea = window.width() * window.height();
if (((float)widgetArea / (float)desktopArea) < 0.75f)
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.
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.