circular includes or something

This is a discussion on circular includes or something within the C++ Programming forums, part of the General Programming Boards category; I need a way to allow my program to do the following, but I can't seem to set things up ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    21,714
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 09: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

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