Thread: Aggregation question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Aggregation question

    I have a Neighborhood class and a House class. The Neighborhood class includes House.h because it needs to contain a vector<House>. But I also want each House to be able to answer "What neighborhood do you belong to?" I think I need to have a Neighborhood field for each House object, but if I include Neighborhood.h in House, I get an infinite loop of includes. So how could I go about getting House to know about Neighborhood without getting that loop of #includes?

  2. #2
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    You should be using the preprocessor to ensure that nothing gets included more than once.


    ie

    Code:
    #ifndef HOME_HPP
    #define HOME_HPP
    #include "Home.hpp"
    /**
    Code here
    **/
    #endif // HOME_HPP
    For a more detailed explanation take a look at the wikipedia page on include guards.

    Regards,

    cs_student
    Last edited by cs_student; 11-11-2009 at 08:53 PM.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use a forward declaration and a pointer to a Neighborhood.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    For an example check the section called Cyclic Dependency here:
    C++ Header File Include Patterns

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Well I have the #ifndef, #define, and #endif lines in both .h files. Maybe I'm using them wrong? Here are the two .h files

    Code:
    //Neighborhood.h
    #include <vector>
    using std::vector;
    #include <string>
    using std::string;
    #include "House.h"
    #ifndef NEIGHBORHOOD_H
    #define NEIGHBRHOOD_H
    #pragma once
    
    class Neighborhood
    {
    public:
    	Neighborhood(string);
    	Neighborhood(string, int, int);
    	Neighborhood(string, int, int, vector<House>&);
    	Neighborhood(string, vector<House>&);
    	~Neighborhood(void);
    
    	int getLength();
    	int getWidth();
    	int getAvailableL();
    	int getAvailableW();
    	void setAvailableL(int);
    	void setAvailableW(int);
    
    	vector<House>& getHouseVec();
    private:
    	string name;
    	int length;
    	int width;
    	int availableL;
    	int availableW;
    	int houseCount;
    	vector<House> houses;
    
    };
    
    #endif
    Code:
    //House.h
    #include <iostream>
    using std::cout;
    using std::endl;
    #include <vector>
    using std::vector;
    #include "HouseItem.h"
    #include "Neighborhood.h"
    #ifndef HOUSE_H
    #define HOUSE_H
    #pragma once
    class House
    {
    public:
    	House(void);
    	House(int);
    	House(int, int);
    	~House(void);
    
    	vector<HouseItem>& getItemVec();
    	int getRooms();
    	int getLength();
    	int getWidth();
    private:
    	vector<HouseItem> items;
    	int rooms;
    	int length;
    	int width;
    
    };
    #endif

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    117
    Hmm well I just changed House.h to having
    Code:
    #pragma once
    
    class Neighborhood;
    	
    class House
    and
    Code:
    private:
    	Neighborhood *hood;
    which compiles. But why wouldn't the include guards stop the loop between the two .h files?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    You are not using include guards correctly. Please read the link I posted. It shows proper usage of include guards and proper handling of cyclic dependencies (which include guards will NOT help with). Specifically, the include guards should be the FIRST thing in your header file.

    You can forward declare an empty version of a class and reference it in your code as long as:

    1. You properly define the complete class later on
    2. You don't use it in a manner where the compiler needs to know how large the class is before it is properly defined


    For example:
    Code:
    This is WRONG:
    
    class Neighborhood;
    
    class House
    {
       private:
          // The compiler would need to know how much space this variable
          // takes up, and thus would need to know how large a Neighborhood
          // object is.
          Neighborhood myNeighborhood;
    };
    
    This is OK:
    
    class Neighborhood;
    
    class House
    {
       private:
          // This is ok because the compiler doesn't need to know how big
          // a Neighborhood object is.  It only needs to know how large a
          // pointer is.
          Neighborhood *myNeighborhood;
    };

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not just about the compiler though. Logically, a house does not contain a neighbourhood, so storing a neighbourhood in a house does not make sense, but allowing a house to have a pointer (or reference) to the neighbourhood that it is in may make sense.
    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. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM