Why i cant create instance of the simpliest custom class?
-
wrote on 10 Jun 2020, 23:15 last edited by Engelard 6 Nov 2020, 07:40
I'm back to Qt after long time, and can't handle that old simple issue when creating custom class which inherits QObject. Class should be a worker class for QThread, but now, even when it's empty and just created it not allowing me create a simple object of my "worker" class.
In the counstructor of my QMainWindow:
tWorker = new timeWorker;
romnysoftware is a simple QMainWindow project, also just created, empty. The only thing i understand from that error handler - that .obj file of it is missing.
-
@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.
wrote on 11 Jun 2020, 10:17 last edited by JonB 6 Nov 2020, 10:21By 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....
-
wrote on 10 Jun 2020, 23:42 last edited by Bonnie 6 Oct 2020, 23:42
Seems that you didn't implement the constructor of timeWorker.
-
wrote on 11 Jun 2020, 07:40 last edited by
@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:
-
wrote on 11 Jun 2020, 07:46 last edited by
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 -
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 -
wrote on 11 Jun 2020, 08:44 last edited by
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++
-
@VRonin Why i did'nt do anything like this before and everything was fine with empty constructors?
wrote on 11 Jun 2020, 09:04 last edited by@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?
-
@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.
wrote on 11 Jun 2020, 10:17 last edited by JonB 6 Nov 2020, 10:21By 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....
-
wrote on 11 Jun 2020, 10:20 last edited by
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
-
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
wrote on 11 Jun 2020, 11:16 last edited by@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!
-
@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!
wrote on 11 Jun 2020, 11:20 last edited by JonB 6 Nov 2020, 11:23@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. -
@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.
1/13