Results 1 to 6 of 6

Thread: How can i prevent class redefinition problem ?

  1. #1
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How can i prevent class redefinition problem ?

    hi all,

    suppose there are three files a.h,b.h,c.h
    which contain classes a,b,c respectively.
    and let c.h included in b.h and b.h included in a.h.

    Qt Code:
    1. //c.h
    2.  
    3. class c
    4. { ....
    5. };
    6. //b.h
    7. #include c.h
    8. class c
    9. {...
    10. }
    11. //a.h
    12. #include b.h
    13. #include c.h
    14. class a
    15. {...
    16. }
    To copy to clipboard, switch view to plain text mode 

    On compiling there comes an error of class redeclaration ..... how can we prevent this ?
    Do what u r afraid to do, and the death of fear is sure.

  2. #2
    Join Date
    Mar 2006
    Location
    Vadodara, Gujarat, India
    Posts
    65
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can i prevent class redefinition problem ?

    ok putting the b.h file in #ifndef ... #endif block solved the problem ...
    Do what u r afraid to do, and the death of fear is sure.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How can i prevent class redefinition problem ?

    Quote Originally Posted by ankurjain
    hi all,

    suppose there are three files a.h,b.h,c.h
    which contain classes a,b,c respectively.
    and let c.h included in b.h and b.h included in a.h.

    Qt Code:
    1. //c.h
    2.  
    3. class c
    4. { ....
    5. };
    6. //b.h
    7. #include c.h
    8. class c
    9. {...
    10. }
    11. //a.h
    12. #include b.h
    13. #include c.h
    14. class a
    15. {...
    16. }
    To copy to clipboard, switch view to plain text mode 

    On compiling there comes an error of class redeclaration ..... how can we prevent this ?
    The standard way is

    Generic header file
    Qt Code:
    1. #ifndef <FILENAME>_H
    2. #define <FILENAME>_H
    3.  
    4. class <ClassName>
    5. {
    6. };
    7. #endif // <FILENAME>_H
    To copy to clipboard, switch view to plain text mode 

    In example
    Qt Code:
    1. // a.h
    2. #ifndef a_h
    3. #define a_h
    4.  
    5. class a
    6. {
    7.  
    8. };
    9. #endif // a_h
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // b.h
    2. #ifndef b_h
    3. #define b_h
    4.  
    5. #include "a.h"
    6.  
    7. class b
    8. {
    9.  
    10. };
    11. #endif // b_h
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // c.h
    2. #ifndef c_h
    3. #define c_h
    4.  
    5. #include "a.h"
    6. #include "b.h"
    7.  
    8. class c
    9. {
    10.  
    11. };
    12. #endif // c_h
    To copy to clipboard, switch view to plain text mode 

    In this way the second inclusion of "a.h" are ignored by precompiler.
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How can i prevent class redefinition problem ?

    If you use Visual C++, you can also try:
    #pragma once
    at the top of each header file, this makes the compiler read the header just once.
    Software Engineer



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

    Default Re: How can i prevent class redefinition problem ?

    Quote Originally Posted by gfunk
    If you use Visual C++, you can also try:
    #pragma once
    at the top of each header file, this makes the compiler read the header just once.
    Beware that this solution isn't portable.

  6. #6
    Join Date
    May 2006
    Posts
    32
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: How can i prevent class redefinition problem ?

    Put all Class declaration in One generic header file.
    such as:
    Qt Code:
    1. //className.h
    2. #ifndef CLASS_NAME_H
    3. #define CLASS_NAME_H
    4. class a;
    5. class b;
    6. class c;
    7. //....
    8.  
    9. #endif //CLASS_NAME_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 4th May 2006, 19:17
  2. Thread Problem
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2006, 17:31
  3. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Compilation problem, don't know why
    By yellowmat in forum Newbie
    Replies: 6
    Last Post: 2nd March 2006, 15:36

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.