circular #include file bug

Started by
4 comments, last by GOOSEDUMP 22 years, 6 months ago
Hey guys/gals... I''m not sure if I have this bug but I would love to know more about it to make sure... file "one.h"
  

//in "one.h"


#ifndef _ONE_
#define _ONE_

#include "two.h"

class one
{
     public:
          one();

     private:
          two* m_two;
};

#endif

  
file "two.h"
  

//in "two.h"


#ifndef _TWO_
#define _TWO_

#include "one.h"

class two
{
     public:
          two();

     private:
          one* m_one;
};

#endif

  
is this the legendary circular #include file bug? If not, what is it? Thanks in advace!
Advertisement
What do you mean by bug? You will get a semantic error because you use a class that has not been declared (which class depends on which of the headers you include in a c/cpp file).

Write a class forward declaration before declaring the class in each file and it should work. If you do you don't even have to include the header of the other class, just make sure to not write any declarations or inline code that depends on knowing the complete type of the other class.

i.e.
          //in "one.h"#ifndef _ONE_ #define _ONE_   class two;  class one{     public:          one();      private:          two* m_two;};#endif  

  //in "two.h"#ifndef _TWO_ #define _TWO_   class one;  class two{     public:          two();       private:          one* m_one;};#endif          


Edited by - Dactylos on October 18, 2001 4:47:44 PM
It would be a "circular #include file bug" if you have no #defines. But the #ifndef-#define statements are there so a header file, once the constant is defined, isn''t included a second time.
Try it out without the #ifndefs and you''ll get an error.

baumep
baumep
Yeah, I know, but that would be a compile time error. I always thought of a bug as an error that appears at runtime. But I guess people have different definitions of what a "bug" is.
This kind of bug wouldn''t be even a compile time bug. It is more a preprocessor bug because all preprocessor commands (beginning with #) are parsed by ...the preprocessor and if you had an error the program wouldn''t even begin to compile.
It isn''t even a bug, it is more an error which is recognised.

baumep
baumep
quote:Original post by baumep
This kind of bug wouldn''t be even a compile time bug. It is more a preprocessor bug because all preprocessor commands (beginning with #) are parsed by ...the preprocessor and if you had an error the program wouldn''t even begin to compile.

I said compile time, because the preprocessor is generally run as a pass in the compiler (though not always).
quote:
It isn''t even a bug, it is more an error which is recognised.

I''m glad we agree

This topic is closed to new replies.

Advertisement