Results 1 to 5 of 5

Thread: #include woes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    #include woes

    It's been a while since I've used C++, and I've gone a little rusty. One problem I managed to overcome in the past was situations where two header files include each other... For example

    In resource.h
    Code:
    #include "resourcemanager.h"
    In resourcemanager.h
    Code:
    #include "resource.h"
    ... but now I've forgot how to get around it. Can anyone help me out?
    Using Visual Studio .NET 2005

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: #include woes

    There is no easy way around this. One common source of this problem is when two classes need each others declaration. In that case use a forward declaration of one of the classes:
    Code:
    // b.h
    class A;
    
    class B {
       A* doSomething();
    }
    
    // a.h
    #include "b.h"
    
    class A {
       B* doSomething();
    }

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: #include woes

    Thanks! That was the method that I used previously (but had forgot the syntax for!).
    Using Visual Studio .NET 2005

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    Re: #include woes

    Bah, it turns out to have not worked so well afterall..

    Code:
    error C2027: use of undefined type 'cResource'
    I'm guessing that is related to the forward declaration. The problem is that each class needs to access the members of the other class, and so I don't see any way around it.

    Any other ideas?
    Using Visual Studio .NET 2005

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: #include woes

    Put the member accesses into the source files.
    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