circular includes?

This is a discussion on circular includes? within the C++ Programming forums, part of the General Programming Boards category; I'm doing work and I think the trouble I'm having is with circular includes. I have Unit as a base ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Question circular includes?

    I'm doing work and I think the trouble I'm having is with circular includes.

    I have Unit as a base class, and Hero and Army both derive from Unit.
    Hero needs to have a vector of Army*'s, and Army needs to have a single Hero*. Here is how the code is setup:
    Code:
    #ifndef ARMY_H
    #define ARMY_H
    
    #include "Unit.h"
    #include "Hero.h"
    
    using namespace std;
    
    class Army : public Unit
    {
    public:
    	Hero* getHero(){return phero;}
    	void setHero(Hero *theHero){phero = theHero;}
    
    private:
    	Hero *phero;
    // ... etc
    and
    Code:
    #ifndef HERO_H
    #define HERO_H
    
    #include<iostream>
    #include "Army.h"
    #include <string>
    #include<vector>
    
    using namespace std;
    
    class Hero : public Unit
    {
    public:
    	void addArmy(Army*);
    	void removeArmy(Army*);
    private:
    	vector<Army*> army;
    // ... etc
    But when it's trying to compile Hero, it sees that Army is included, so it tries to compile that, which says that Hero is included, etc. So I believe I've reached a circular include.

    Is there any way to achieve this functionality without doing a restructure of my classes? Thanks for help.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    nevermind, i found a way through in intermediary class

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,195
    Intermediary class is one way. You might also want to do a search for "forward declaration".

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    How do you declare an object when two classes have pointers to each other?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by 7stud
    How do you declare an object when two classes have pointers to each other?
    Often a forward declaration is used.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    declare a class <---->declare an object.

    How do you declare an object of one of the classes in main()?
    Code:
    #include <iostream>
    using namespace std;
    
    using namespace std;
    
    class B;
    
    class A
    {
    public:
    	A(B* ptr, int n)
    	{
    		pB = ptr;
    		num = n;
    	}
    
    	int get()
    	{
    		return num;
    	}
    
    private:
    	B* pB;
    	int num;
    };
    
    class B
    {
    public:
    	B(A* ptr, int n)
    	{
    		pA = ptr;
    		num = n;
    	}
    
    	int get()
    	{
    		return num;
    	}
    private:
    
    	A* pA;
    	int num;
    };
    
    
    int main()
    {
    	
    
        return 0;
    }
    Do you have to use a default constructor?
    Last edited by 7stud; 02-25-2006 at 08:30 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by 7stud
    How do you declare an object when two classes have pointers to each other?
    I guess define might have been a better term.

    Declare a class; define an object.

    If one has to exist before the other, and it needs to point to something that may not yet exist, I'd say that a default constructor should be necessary for at least one of the them. But perhaps it depends on the scope of the objects.
    Code:
    #include <iostream>
    using namespace std;
    
    class B;
    
    class A
    {
       B* pB;
    public:
       A(B* ptr) : pB(ptr) {}
       friend ostream& operator<< (ostream& o, const A &a)
       {
          return o << a.pB;
       }
    };
    
    class B
    {
       A* pA;
    public:
       B(A* ptr) : pA(ptr) {}
       friend ostream& operator<< (ostream& o, const B &b)
       {
          return o << b.pA;
       }
    };
    
    extern B b;
    A a(&b);
    B b(&a);
    
    int main()
    {
       cout << a << endl;
       cout << b << endl;
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. circular includes or something
    By dudeomanodude in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2008, 11:38 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Circular include issue
    By einarp in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2006, 08:43 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