Solved Why i cant create instance of the simpliest custom class?
-
Seems that you didn't implement the constructor of timeWorker.
-
@Bonnie Yes, problem with that, but i peeked on my former projects, and in those, worker classes have empty constructors, and everything there looks the same. But there it works, and in this new it does not. Thats how my class looks like:
-
timeWorker();
is not an empty constructor, it's a consructor declaration. You are telling the compiler to trust you that the constructor exists but if you don't actually implement it (empty or not) than the compiler complains because you lied to it.If you don't have to do anything in the constructor, however, the easiest way is typing
using QObject::QObject;
instead of decalring constructors, this tells the compiler to just use QObject constructors for your class -
@VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?
-
Again. That's not an empty constructor.
This is an empty constructor:timeWorker(){}
This is a constructor declarationtimeWorker();
This is immutable from the dawn of C++
-
@Engelard said in Why i cant create instance of the simpliest custom class?:
@VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?
As @VRonin says, so why don't you show us what you did before which worked?
-
@JonB I'd show you completelly the same. Empty constructors of the worker class, but those working.
By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.
-
By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.
What you have elsewhere in your code might be obvious to you but is not obvious to us when we try to answer user questions. Do you mean that in addition to the
.h
file you show you do also haveworker::worker() { }
in the
.cpp
file?Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the
debug
/release
compilation directory and re-runqmake
on your project? Especially if you, say, add or removeQ_OBJECT
from the header, but also other cases, so make sure you have done this.Further, from the error message I see it mentions "file not found", don't know if that is relevant. And it is looking for a constructor which accepts a
class QObject *
parent, like https://doc.qt.io/qt-5/qobject.html#QObject does, which yourtimeWorker()
does not.Thoughts for you, only trying to be helpful....
-
Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed
-
@VRonin said in Why i cant create instance of the simpliest custom class?:
Unless you have a definition somewhere else (usually in the .cpp file) a constructur declaration is not enough. This is not related to Qt, this is pure C++ and it has been this way since the language existed
I ofc have the proper declaration and definition, as i said i have bunch of previous projects which made exactly that way, and those working.
Cutting a long story short, if you feel your code is correct and you are getting irritated by these questions, have you deleted all files from the
debug
/release
compilation directory and re-runqmake
on your project?Irritated indeed, because i know the basics of C++ well, and i also know for 100% that i'm doing everything right but it get to me like something wrong, and it is - again Qt bug(or call that whatever you preffer but it is a bug).
I deleted debug-release folders, Rerun Qmake and now it SUDDENLY tell me that no errors and everything run fine!
-
@Engelard
That's why I suggested the clean-out-and-re-make! It's not a Qt bug, but it is a problem with Qt Creator/qmake.It is just life/how it is that when you find inexplicable compilation/link errors it may be that cached previous compilation stuff is the culprit. I don't like it any more than you do, but I often find I have to do this to get it to sort itself out, I agree it should be more robust at noticing changes.... I happen to know, I believe, that for example adding
Q_OBJECT
to an existing.h
file seems to require clean-and-remake. -
@JonB Tnx for reminding me about that delete/rebuild way.