Results 1 to 3 of 3

Thread: recursive #include calls

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    84

    recursive #include calls

    I have two classes that are codependant. They both have functions that call instances of the other class.
    Each class is defined in its own header file. Say Class1.h and Class2.h

    Right now I have #include "Class2.h" in my Class1.h file and #include "Class1.h" in my Class2.h file.

    Naturally when I compile its falls into a small recursive loop before returning a C2061: syntax error.

    each header file has code such that it only compiles once.

    I'm using VC 6.0 is there a simple way to solve this problem short of using a lib solution?

    Thanks in advance.

  2. #2
    jim mcnamara
    Guest
    use directives to check on what has been declared:
    Code:
    #ifndef MYCLASS1
    #define MYCLASS1 
    ..... do your thing here
    #endif

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Put the real code of the classes in a .cpp file and leave only the declarations in the header. Then add forward declarations before both classes:

    class A;

    class B
    {
    A* pA;
    };


    In the cpp files you simply include both headers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width