Circular includes

This is a discussion on Circular includes within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a simple problem, most likely faced by someone before. I have 3 classes Cell, Juxtacrine and Morphogen. ...

  1. #1
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92

    Circular includes

    Hi, I have a simple problem, most likely faced by someone before.

    I have 3 classes Cell, Juxtacrine and Morphogen. Juxtacrine is derived from Cell, and Morphogen is derived from Cell also.

    Basically the two derived classes both include the Cell.h to find the definition of Cell.

    The problem comes when i need to use the Objects in the main function eg

    Code:
    #include "Juxtacrine.h"
    #include "Morphogen.h"
    
    int main()
    {
    	Juxtacrine j;
    	Morphogen m;
    	return 0;
    }
    This obviously causes the circular include, and was wondering if there is a way to use preprocessor directives to stop this happening eg

    Code:
    #ifndef CELL_H
    #include "Cell.h"
    #endif
    But this doesnt seem to fix it. Has anyone got an idea how to get around this? I dont really want to have to work out what class includes what as ill have several classes with multiple inheritence later on, which will cause a headache working out the circular includes if i do it manually...

    Heres my code if u need it...

    Code:
    class Cell 
    {
      public:
    
    	Cell();
    	~Cell();
    
      private:
    
    	virtual void signal();
    };
    Code:
    #include "Cell.h"
    
    class Juxtacrine : public Cell
    {
    
      public:
    
    	Juxtacrine();
    	~Juxtacrine();
    
      private :
    
    	void signal();
    };
    Code:
    #include "Cell.h"
    
    class Morphogen : public Cell
    {
    
      public:
    
    	Morphogen();
    	~Morphogen();
    
      private :
    
    	void signal();
    };
    Im Using WinXP / VC++ (if needed)

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    #ifndef CELL_H
    #include "Cell.h"
    #endif
    should be
    Code:
    #ifndef CELL_H
    #define CELL_H
    #include "Cell.h"
    #endif
    But this would go in the header itself. so the source code would be:
    Code:
     #include "Cell.h"
    #include <blah blah blah>
    and inside of the header you would have
    Code:
    #ifndef CELL_H
    #define CELL_H
    
    // All your definations and such
    
    #endif

  3. #3
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Excellent, thats fixed it ! Thanks!

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. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 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 Raybdbomb in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2006, 09:40 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