Destructor implemetation for a class that inherits QObject
-
Hello forum members,
I have a class "Con"with a default destructor in the header defined as: (filtered display)
class CON_EXPORT Con : public QObject { Q_OBJECT Q_DISABLE_COPY_MOVE(Con ); public: Con(); explicit Con(std::shared_ptr<Client> client); ~Con() override; In the CPP: Con::Con() : Con(std::make_shared<Client>()) { } Con::Con(std::shared_ptr<Client> client) : m_client(std::move(client)) { ........... } Con::~Con() = default;
This is causing linker error in a Windows compilation.
mocs_compilation_Debug.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QObject::`scalar deleting destructor'(unsigned int)" (??_GQObject@@UEAAPEAXI@Z) [C: \mpi-api\build\src\mb\mpi\mpi-common\conan\conan.vcxproj] Con.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QObject::`scalar deleting destructor'(unsigned int)" (??_GQObject@@UEAAPEAXI@Z) [C:\mpi-api\build\src\mb\mpi\mpi-common\conan\conan.vcxproj] mocs_compilation_Debug.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QObject::`vector deleting destructor'(unsigned int)" (??_EQObject@@UEAAPEAXI@Z) [C: \mpi-api\build\src\mb\mpi\mpi-common\conan\conan.vcxproj] Con.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QObject::`vector deleting destructor'(unsigned int)" (??_EQObject@@UEAAPEAXI@Z) [C:\mpi-api\bu ild\src\mb\mpi\mpi-common\conan\conan.vcxproj]
On reflection, it seems that the destructor is not able to find the specific implementations it is expecting to cover the diverse types : one for "scalar" and another for "vector".
Now as a I write, I see that there is no 1:1 correspondence between constructor (there are 2 of them) and destructor (only one default). I though marking it override would be enough. Am I missing something? A Q_<macro> missing or something?
-
You don't need the dtor at all - remove it and see if the problem still exists.
/edit: when you don't add the ctor you must include client.h in your Con.h otherwise you will get a compiler error but for testing it should be ok.
/edit2:
Please post your real code, yours does not compile:Con(); explicit Con(std::shared_ptr<Client> client); Con();
-
You don't need the dtor at all - remove it and see if the problem still exists.
/edit: when you don't add the ctor you must include client.h in your Con.h otherwise you will get a compiler error but for testing it should be ok.
/edit2:
Please post your real code, yours does not compile:Con(); explicit Con(std::shared_ptr<Client> client); Con();
@Christian-Ehrlicher , apologies about not having full compilable code attached.
I will try in future isolate compilable files. My original post was intended to show code snippets, I editted out the the duplicate line.
Anyway, I tried removing the destructor. The problem is the same.
The post gives a good hint:
https://stackoverflow.com/questions/42449063/vs-c-dll-scalar-deleteing-destructorIt hints that the solution lies in looking at the code where an object of the class is used. That is the moment the constructor/destructor impl. are sought after.
-
You have to export the whole class, not only a few members. Otherwise your missing function is not visible when using the library.
-
You have to export the whole class, not only a few members. Otherwise your missing function is not visible when using the library.
I do the export in my class:
class CON_EXPORT Con : public QObject {
Its as if the the VC++ generated functions (scalar/vector deleting destructor) do not exist in the Qt library so the linker cant find them
-
I do the export in my class:
class CON_EXPORT Con : public QObject {
Its as if the the VC++ generated functions (scalar/vector deleting destructor) do not exist in the Qt library so the linker cant find them
@ReneUafasah said in Destructor implemetation for a class that inherits QObject:
I do the export in my class:
Not in the stackoverflow post...
You really should post real code instead some copy'n'pasted stuff where only the half is correct...
-
Sorry, but it not possible attach the all off my class, it is too big..
Just stepping back a bit, and focusing on the export part, I have implemented in the following way:#include "mysharedlib_global.h" class MYSHAREDLIB_EXPORT MyClass...
as described in https://doc.qt.io/qt-5/sharedlibrary.html
I dont think I need to show all the header.
add_definitions(-DMYSHAREDLIB_LIBRARY).
Internally there are two generated destructor variants which the Qt library cant find.
con_add_library(con SOURCES ${sources} DEPENDENCIES ${linked_libraries})
-
Then do what every software developer has to do in this case - reduce your testcase until the error no longer occurs or until it's small enough to post it here. That's software development.