Thread: circular includes or something

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    circular includes or something

    I need a way to allow my program to do the following, but I can't seem to set things up (either in the includes or elsewhere) to allow it to do this:

    foo.h
    Code:
     
    class Status{
     
      public:
        Status( bool status ) : m_status( status ) {}
        const bool getMStatus() const { return m_status; }
     
      private:
        bool m_status;
     
      friend Bar::Bar();
    };
     
    class Foo{
     
      public:
        Foo():fooStatus(0){}
        const bool getStatus() const { return fooStatus.getMStatus(); }
     
        Status& getStatusObject(){ return fooStatus; }
     
      private:
        Status fooStatus;
    }
    bar.h
    Code:
    class Bar{
     
      public:
        Bar() :{ mFoo.getStatusObject().m_status = 0; }
     
      private:
        Foo mFoo;
    };
    Problem is the compiler complains about "friend Bar::Bar()" because Bar hasn't been declared. I've tried adding both of these to the top of foo.h, but to no avail:

    Code:
    class Bar; // Compiler complains about no definition
    Code:
    #include "bar.h" // Compiler complains in bar.h that Foo isn't defined
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by dudeomanodude View Post
    Problem is the compiler complains about "friend Bar::Bar()" because Bar hasn't been declared. I've tried adding both of these to the top of foo.h, but to no avail:

    Code:
    class Bar; // Compiler complains about no definition
    The forward definition is insufficient, because you are declaring Bar::Bar() to be a friend, which requires knowledge of the inside of Bar. Instead of making just the constructor a friend, make the whole class Bar a friend, and the forward declaration should work.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... I notice that at the moment friendship is entirely unnecessary: the default constructor of Foo does what the Bar default constructor is trying to do, namely, set m_status to false.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Circular include issue
    By einarp in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2006, 08:43 PM
  3. circular includes?
    By Raybdbomb in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 10:40 PM
  4. Circular includes
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2004, 05:43 PM
  5. Circular #includes
    By Inquirer in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2002, 11:07 PM