Results 1 to 6 of 6

Thread: Problem with Destructor of QObject

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with Destructor of QObject

    i get the Message
    "neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined." for a class that is derived from QObject where i am trying to have a destructor.
    and i just can't figure out what it means and what i could do against it. Could somebody please enlighten me?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Destructor of QObject

    Could you post the exact error message and the part of your code it refers to? How did you define your class?

  3. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Destructor of QObject

    that is allready the complete errormessage. it refered to the point in the destructor were i was using delete on a pointer that i store as a private attribute to my class. Now that i have commented out that line i don't get this error anymore.
    It seems the problem is, that the pointer points to an instance of a class which i had to forward declarate. Not being experienced in C++ i am fighting with a DoD-Problem here:

    i have a class which depicts an asset (for example a pump or a pipe or else). The assets can have attributes like length, width and things like that. But the attributes can also be links to a different asset (as a pipe would have to know of the two manholes between it is to get the depth) or they could be keys for a details-Table. Therefore there are 4 Attribute-Type-Classes which all inherit a masterclass attribute. Then i have a third class with costs. For a pipe one of the costs would be the materialprice of the pipe itself. So this instance of the costclass should know that the price can be calculated with lengthOfPipe * PriceFor1m and it should of course also know where to find the price for 1m

    Now the problem:
    I want to be able to ask the assetclass what this asset costs. So the asset will go through all the cost-classes attached to her (actually the asset class stores a QMap<QString,cCost> and ask each of those costs what they cost and sum it up. To do that those costs will need to get information about the asset: in the example above the length of the pipe is stored with the asset.
    So both classes need to use each others methods, i either get a DoD or Problems like the above ;(

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Destructor of QObject

    Quote Originally Posted by mikro
    the pointer points to an instance of a class which i had to forward declarate.
    If you have only forward declaration, then you can't invoke any method, constructor or destructor of that class. Compiler simply doesn't know if they exist. You must add proper #include directive.

    Quote Originally Posted by mikro
    So both classes need to use each others methods, i either get a DoD or Problems like the above ;(
    You can solve it with forward declarations, like this:
    Qt Code:
    1. // a.h
    2. class B; // forward declaration only, so you can't use B instances here
    3.  
    4. class A
    5. {
    6. ...
    7. void someMethod( const B& b ); // this will work
    8. void someOtherMethod( B* b ); // this is also ok
    9. ...
    10. private:
    11. B *_b; // note that you can use only a pointer here, since you can't include class definition
    12. };
    13.  
    14. //-------------------------------------------------------------------
    15. // b.h
    16.  
    17. class A;
    18.  
    19. class B
    20. {
    21. ...
    22. private:
    23. A *_a;
    24. };
    25.  
    26. //-------------------------------------------------------------------
    27. // a.cpp
    28.  
    29. #include "a.h"
    30. #include "b.h"
    31.  
    32. vouid A::someMethod( const B& b )
    33. {
    34. // you can use B here
    35. }
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jacek for this useful post:

    mikro (9th July 2006)

  6. #5
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Destructor of QObject

    part of me wanted to say "yes, i know all that" but actually you have given me the answer: my problem was that i didn't realize, that just because i cannot have both headerfiles include the other doesn't mean that i cannot include them both in the .cpp and there use all the methods of the other class i need.
    nevertheless i still don't understand why there should be a problem with deleting that pointer and neither what that errormessage was suppossed to tell me.
    all i had was:
    Qt Code:
    1. anlage.h:
    2.  
    3. #include "kosten.h"
    4. class cAttribut;
    5. class cMassnahme;
    6.  
    7. class cAnlage : public QTreeWidgetItem {
    8.  
    9. public:
    10. ~cAnlage() {
    11. if (this->isDirty) {
    12. writeDB();
    13. }
    14. delete anlagenart;
    15. // delete mMassnahme;
    16. // qDeleteAll(Attribute);
    17. qDeleteAll(kosten);
    18. };
    19. protected:
    20. QMap<QString,cAttribut*> Attribute;
    21. QMap<QString,cKosten*> kosten;
    22. private:
    23. bool isDirty;
    24. cMassnahme* mMassnahme;
    To copy to clipboard, switch view to plain text mode 
    cAttribut and cMassnahme are only forward-declarated as i need to fully include this headerfile in their headers, whereas cKosten is in the included file kosten.h. As you can see i had to comment out the lines deleting the pointers to those classes that had only been forward declarated whereas i can still delete kosten.

    I have now put the implementation of the destructor over to the .cpp where i can include the header and it works
    is it normal that you can't delete a pointer of a type that is only forward-declarated?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Destructor of QObject

    Quote Originally Posted by mikro
    is it normal that you can't delete a pointer of a type that is only forward-declarated?
    Yes, forward declaration only tells the compiler that such class exists, but the compiler has to know where exactly the destructor is located to be able to use it --- forward declaration doesn't provide this information.

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. subclassing/inheritance QObject problem
    By cbeall1 in forum Qt Programming
    Replies: 12
    Last Post: 13th February 2006, 17:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.