Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Destructor implemetation for a class that inherits QObject
Forum Updated to NodeBB v4.3 + New Features

Destructor implemetation for a class that inherits QObject

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 981 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ReneUafasahR Offline
    ReneUafasahR Offline
    ReneUafasah
    wrote on last edited by ReneUafasah
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      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();
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      ReneUafasahR 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        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();
        
        ReneUafasahR Offline
        ReneUafasahR Offline
        ReneUafasah
        wrote on last edited by
        #3

        @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-destructor

        It 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.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You have to export the whole class, not only a few members. Otherwise your missing function is not visible when using the library.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          ReneUafasahR 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            You have to export the whole class, not only a few members. Otherwise your missing function is not visible when using the library.

            ReneUafasahR Offline
            ReneUafasahR Offline
            ReneUafasah
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            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

            Christian EhrlicherC 1 Reply Last reply
            0
            • ReneUafasahR ReneUafasah

              @Christian-Ehrlicher

              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

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • ReneUafasahR Offline
                ReneUafasahR Offline
                ReneUafasah
                wrote on last edited by
                #7

                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})

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved