Results 1 to 7 of 7

Thread: around circular dependencies

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default around circular dependencies

    I have written intentionally two classes , 1 instance 2 and 2 instance 1.
    My intention is discover if the project can build or not.
    ok, the project can be compiled without errors.

    But when run I have :
    exited with code -1073741819 = STATUS_ACCESS_VIOLATION 0xC0000005

    Can I'm sure that this message is because the circular dependencies?
    Why not I get errors when build ? (I use mingw)
    Thanks.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: around circular dependencies

    Are you using forward declarations ?
    Qt Code:
    1. // A.h:
    2. class B; //!< declare class B, just to be able to use pointers
    3.  
    4. class A{
    5. public:
    6. A();
    7. protected:
    8. B * b;
    9. };
    10.  
    11. // A.cpp
    12. #include "A.h"
    13. #include "B.h"
    14.  
    15. A::A(){
    16. b = new B();
    17. }
    18.  
    19. ///////// same for class B //////////////
    20.  
    21. // B.h:
    22. class A; //!< forward declare class A
    23.  
    24. class B{
    25. public:
    26. B();
    27. protected:
    28. A * a;
    29. };
    30.  
    31. // B.cpp
    32. #include "B.h"
    33. #include "A.h"
    34.  
    35. B::B(){
    36. a = new A();
    37. }
    To copy to clipboard, switch view to plain text mode 

    This way there are no "circular" includes.
    Show us how did you code it

  3. #3
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: around circular dependencies

    I'm have not forward intentionally.
    My question are 'why the project can be build and have no errors' ?
    I have not tools in my QT+mingw to check it?
    Thanks

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: around circular dependencies

    I meant that forward declaring is somethig that can help you when you want to have class A using class B and the other way around.
    How did you include the headers ? If you did somethig like:
    Qt Code:
    1. // A.h:
    2. #ifndef A
    3. #define A
    4.  
    5. #include "B.h"
    6.  
    7. class A...
    8.  
    9. #endif
    10.  
    11. // B.h:
    12.  
    13. #ifndef B
    14. #define B
    15.  
    16. #include "A.h"
    17.  
    18. class B...
    19.  
    20. #endif
    To copy to clipboard, switch view to plain text mode 
    then think about it, in order to use B in A, B should be declared before A ( and the other way around ). Which class is defined first and which is second in this case ? ( without seeing your include schema I can only guess how this looks like in your code )
    Try the "forward declarations" solution proposed earlier.

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: around circular dependencies

    Yes, something like this.
    As I said at the beggining of the post, I deliberately have write a code to rise an error with circular dependencies.
    And my questión is: How can be possible I can build the project?
    And this error means circular dependence error ?
    exited with code -1073741819 = STATUS_ACCESS_VIOLATION 0xC0000005


    and, a more question:


    ClassA.h
    Qt Code:
    1. class B;
    2. class A {
    3. public:
    4. private:
    5. }
    To copy to clipboard, switch view to plain text mode 
    ClassA.cpp
    Qt Code:
    1. #include "classb.h"
    2. ClassA::function(){
    3. Classb *clasb;
    4. clasb->function();
    5. }
    To copy to clipboard, switch view to plain text mode 
    It works .

    But this one not :
    ClassA.h
    Qt Code:
    1. class B;
    2. class A {
    3. public:
    4. Classb *clasb; // I want to use in several places ...
    5. private:
    6. }
    To copy to clipboard, switch view to plain text mode 
    ClassA.cpp
    Qt Code:
    1. #include "classb.h"
    2. ClassA::function(){
    3. clasb->function();
    4. }
    To copy to clipboard, switch view to plain text mode 

    At the second case, I have :
    Invalid use in static ClassA::clasb static member.

    Is that than I cannot create public or private objetcs using forward?
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: around circular dependencies

    If you are using an uninitialized pointer then why are you wondering why it crashes?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: around circular dependencies

    Quote Originally Posted by tonnot View Post

    Qt Code:
    1. #include "classb.h"
    2. ClassA::function(){
    3. clasb->function();
    4. }
    To copy to clipboard, switch view to plain text mode 
    At the second case, I have :
    Invalid use in static ClassA::clasb static member.
    The above code fragment doesn't make sense at all. Forward declaration doesn't mean that you will declare the body of the function somewhere else. Read starting from here http://www.parashift.com/c++-faq-lit...html#faq-39.11
    Misha R.evolution - High level Debugging IDE

    Programming is about 2 basic principles: KISS and RTFM!!!

Similar Threads

  1. Circular Layout
    By Leolander in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2010, 08:02
  2. Circular QLinkedList
    By dyngoman in forum Qt Programming
    Replies: 1
    Last Post: 24th March 2010, 08:57
  3. circular buttons
    By moabi in forum Newbie
    Replies: 2
    Last Post: 18th March 2010, 04:28
  4. Circular scale
    By fruzzo in forum Qwt
    Replies: 1
    Last Post: 6th March 2008, 07:20
  5. Is there a circular reference here?
    By rh in forum General Programming
    Replies: 3
    Last Post: 22nd January 2006, 21:51

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.