Results 1 to 5 of 5

Thread: association between 2 classes

  1. #1
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default association between 2 classes

    Hello,
    I have 2 classes A and N, and I want an association between them.

    Here is the code :
    Qt Code:
    1. #include "a.h"
    2. #include "n.h"
    3. int main()
    4. {
    5. A* a=new A();
    6. N* n=new N(a);
    7. A->setN(n);
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef A_H
    2. #define A_H
    3.  
    4. #include "n.h"
    5.  
    6. class A{
    7.  
    8. N* _n;
    9.  
    10. public :
    11. A(){}
    12. void setN(N* n){_n=n;
    13. }
    14.  
    15. };
    16. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef N_H
    2. #define N_H
    3.  
    4.  
    5. #include "a.h"
    6. class N{
    7.  
    8. A* _a;
    9.  
    10. public :
    11. N(A* a):_a(a){} <--- syntax error*: ')'
    12.  
    13.  
    14.  
    15. };
    16. #endif
    To copy to clipboard, switch view to plain text mode 
    I have a problem at the compilation ...

  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: association between 2 classes

    Move the implementation to .cpp files and replace #include with forward declarations.

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

    castorvert (25th March 2006)

  4. #3
    Join Date
    Mar 2006
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: association between 2 classes

    The problem here is the infinite recursion of the dependencies in the header files. You cannot include a header file in a header file that includes that header file (read this line again, it makes sense). So, basically, you should either make:
    - a.h independent of n.h
    - n.h independent of a.h
    - a.h independent of n.h and n.h independent of a.h

    This is only needed for header files, not for .cpp files! The .cpp files may include both header files, no problems there ever.
    You don't have to move the implementation to the source files in this case. Pointers can be used in header files without knowing the contents of classes N or A. (As a benefit, your compilation will speed up if you use a class definition, but sometimes it is not that handy)

    So, the following works:
    a.h:
    Qt Code:
    1. #ifndef A_H
    2. #define A_H
    3.  
    4. class N;
    5.  
    6. class A{
    7. N* _n;
    8. public :
    9. A(){}
    10. void setN(N* n){_n=n;
    11. }
    12. };
    13. #endif
    To copy to clipboard, switch view to plain text mode 
    n.h
    Qt Code:
    1. #ifndef N_H
    2. #define N_H
    3.  
    4. class A;
    5.  
    6. class N{
    7. A* _a;
    8. public :
    9. N(A* a):_a(a){}
    10. };
    11. #endif
    To copy to clipboard, switch view to plain text mode 

    And you can keep main.cpp the same.

    Beluvius.

  5. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: association between 2 classes

    I once planned to do everything with forward declarations, hoping it would be a universal solution. But how do I do this with classes that use templates?

    Oh well, if I can't I can't. I can make an exception in that case if I have to.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  6. #5
    Join Date
    Mar 2006
    Posts
    14
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: association between 2 classes

    Don't make it a general rule to use forward declarations. It can be used in many many cases, but sometimes it is simply very usefull to include the actual header file.

    With templates, you can be in luck depending on the implementation. As the template functions are only expanded when called, the conflict may or may not be there. If you do have problems, you should either unlink the two classes, or make a 'master' class which handles the communication between the two classes. I probably don't have to advise you to prevent 2 classes being linked to eachother

Similar Threads

  1. QtTest: Unittesting in several classes
    By Jojo in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2009, 12:38
  2. Professional Classes & Objects Structure
    By webstylemedia in forum Newbie
    Replies: 4
    Last Post: 4th August 2008, 10:50
  3. Design classes in OOP?
    By vql in forum General Programming
    Replies: 5
    Last Post: 25th October 2007, 14:18
  4. Qcj Data classes initial release
    By croftj in forum Qt-based Software
    Replies: 0
    Last Post: 1st May 2007, 02:51
  5. Adding nonQt classes to QtApplication
    By codebehind in forum Newbie
    Replies: 11
    Last Post: 23rd April 2007, 21:08

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.