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

Why i cant create instance of the simpliest custom class?



  • 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;
    

    alt text

    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.



  • @Engelard

    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 have

    worker::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-run qmake on your project? Especially if you, say, add or remove Q_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 your timeWorker() does not.

    Thoughts for you, only trying to be helpful....



  • 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:

    alt text



  • 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 declaration timeWorker();

    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.

    alt text

    By empty i mean it is empty, no need to show worker::worker(){} stuff, i suppose it should be obvious.



  • @Engelard

    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 have

    worker::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-run qmake on your project? Especially if you, say, add or remove Q_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 your timeWorker() 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-run qmake 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.


Log in to reply