class and header inclusion problem.

This is a discussion on class and header inclusion problem. within the C++ Programming forums, part of the General Programming Boards category; Okay, I have two classes, A and B. B uses A, and A uses B. They both have their own ...

  1. #1
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    class and header inclusion problem.

    Okay, I have two classes, A and B. B uses A, and A uses B. They both have their own header and source files... A.cpp, A.hpp, B.cpp and B.hpp

    In A.hpp I include B.hpp and in B.hpp I include A.hpp... this isn't working. I get an infinite recursion error on MSVC++:

    warning C4182: #include nesting level is 361 deep; possible infinite recursion
    fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit

    If I don't include A.hpp in B.hpp or vice versa, I get errors about one class being undefined... How can I solve this?

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Depends on how each class use each other in the .hpp file.
    If it's just a referance or pointer you can do something like this

    Code:
    class A;
    
    class B {
          A& a;
    public:
          B(A& aa) : a(aa) { }
    };

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    That's done it, it's working now. Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21