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 1.0k 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.
  • R Offline
    R Offline
    ReneUafasah
    wrote on 17 Jun 2022, 09:24 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
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 17 Jun 2022, 09:36 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

      R 1 Reply Last reply 17 Jun 2022, 10:37
      0
      • C Christian Ehrlicher
        17 Jun 2022, 09:36

        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();
        
        R Offline
        R Offline
        ReneUafasah
        wrote on 17 Jun 2022, 10:37 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
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 17 Jun 2022, 10:54 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

          R 1 Reply Last reply 17 Jun 2022, 14:45
          1
          • C Christian Ehrlicher
            17 Jun 2022, 10:54

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

            R Offline
            R Offline
            ReneUafasah
            wrote on 17 Jun 2022, 14:45 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

            C 1 Reply Last reply 17 Jun 2022, 14:46
            0
            • R ReneUafasah
              17 Jun 2022, 14:45

              @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

              C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 17 Jun 2022, 14:46 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
              • R Offline
                R Offline
                ReneUafasah
                wrote on 17 Jun 2022, 14:59 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
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 17 Jun 2022, 19:41 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

                  3/8

                  17 Jun 2022, 10:37

                  5 unread
                  • Login

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