Thread: File Inclusion Problems

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    8

    File Inclusion Problems

    I have a program with these four files as part of it.

    point.h
    Code:
    #ifndef POINT_H
    #define POINT_H
    
    #ifndef CIRCC_H
    #define CIRCC_H
    #include "circ.h"
    #endif
    
    class point
    {
    public:
    	// Constructors
    	// Methods
    protected:
    	// More methods and variables
    };
    
    #endif
    point.cpp
    Code:
    #include "point.h"
    // Methods
    circ.h
    Code:
    #ifndef CIRC_H
    #define CIRC_H
    
    #ifndef POINTC_H
    #define POINTC_H
    #include "point.h"
    #endif
    
    class circ:public point
    {
    public:
    	// Constructors
    	// Methods and Variables
    protected:
    	// More variables
    };
    
    #endif
    circ.cpp
    Code:
    #include "circ.h"
    // Methods
    I get the errors

    - c:\users\john\documents\visual studio 2005\projects\ogl3\ogl3\point.h(19) : error C2061: syntax error : identifier 'circ'

    and

    - c:\users\john\documents\visual studio 2005\projects\ogl3\ogl3\circ.h(10) : error C2504: 'point' : base class undefined

    as well as some other errors similar to this. I think it probably has something to do with the fact that both the circ and point classes need to include each other, but unfortunately I don't know how to fix this problem.


    Exact instructions on how to resolve this problem are the most important thing here,
    but I also think the problem comes because I don't really have a good idea of what exactly including files does and how exactly all the files are parsed. I just know that if a file needs to use a certain class or method, you have to include its header file, but debugging problems with it is really difficult for me, so some explanations as to what the linker and compiler are actually doing would be nice.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Delete the red lines
    Code:
    #ifndef POINT_H
    #define POINT_H
    
    #ifndef CIRCC_H
    #define CIRCC_H
    #include "circ.h"
    #endif
    
    class point
    {
    public:
    	// Constructors
    	// Methods
    protected:
    	// More methods and variables
    };
    
    #endif
    Code:
    #ifndef CIRC_H
    #define CIRC_H
    
    #ifndef POINTC_H
    #define POINTC_H
    #include "point.h"
    #endif
    
    class circ:public point
    {
    public:
    	// Constructors
    	// Methods and Variables
    protected:
    	// More variables
    };
    
    #endif
    Now why do you need #include "circ.h" in point.h ?
    Does your point class need to know anything about circ?
    If not, then just delete that line as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You've doubled the include guards. Because of that, and the fact that the second guard for the same file has a different #define (eg, POINTC_H vs POINT_H), you've just reproduced the problem you are trying to prevent: when point.h is preprocessed, it will have defined POINT_H, and include circ.h. Since it hasn't defined POINTC_H, it will now include itself again.

    All that's necessary is this:

    point.h
    Code:
    #ifndef POINT_H
    #define POINT_H
    
    [ the rest of the header ]
    
    #endif
    circ.h
    Code:
    #ifndef CIRC_H
    #define CIRC_H
    
    [ the rest of the header ]
    
    #endif
    One header, one include guard wrapping it. Simple, foolproof. Don't bother with checks and guards for other headers, it will only end in a mess.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    I got rid of the extra inclusion guards and now I have the error

    c:\users\john\documents\visual studio 2005\projects\ogl3\ogl3\point.h(16) : error C2061: syntax error : identifier 'circ'
    circ.cpp

    along with a few errors that stem from this.

    I can't simply remove '#include "circ.h" ' from my point header file because it uses circ as a parameter for one of its methods.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Then you need to think about your design somewhat.

    I mean, if you added a rect class later on, which also used point, would you need to add that as well?

    Consider adding this (in place of the #include) at the start of point.h

    Code:
    class circ;
    It will allow you (for example) to allow point to create/use a pointer to circ without actually knowing what a circ looks like.

    But if you need to create instances of circ inside point, then it's going to get ugly.

    Post some example of how you use circ inside point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dcco View Post
    I can't simply remove '#include "circ.h" ' from my point header file because it uses circ as a parameter for one of its methods.
    This is where the difference between a class definition and a class declaration comes in. You do not have to remove the #include, but like Salem says, use a forward declaration at the very beginning, before you include anything:

    circ.h
    Code:
    #ifndef CIRC_H
    #define CIRC_H
    class circle;   // forward declaration
    #include "point.h"
    
    [class circle definition]
    #endif
    Now, "class circle" has been declared so the type exists if referenced in the "point.h" include. And after that, class point exist if referenced in the class circle definition.

    point.h
    Code:
    #ifndef POINT_H
    #define POINT_H
    class point;  // forward declaration
    #include "circ.h"
    
    [class point definition]
    
    #endif
    You can now include all of your headers in each other if you want
    Last edited by MK27; 06-17-2011 at 03:16 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    this is part of point.h
    Code:
    virtual bool checkCol(circ a);
    and this is part of point.cpp
    Code:
    bool point::checkCol(circ a)
    {
    	return dist(a.x,a.y,x,y) <= a.rad;
    }
    It is the only place where the circ class is being used.

    The context of the checkCol method is that while every object in the program derives in some way or another from the point class, every object that requires collision derives from the circ class. While I should never actually need to check for any collision with any object that isnt a circle, I do need objects that arent circles, and so the data structure storing the list of objects will need points to have a checkCol method for me to use polymorphism and call that method on objects that are circles. While I could simply do a cast or have points be circles with a radius of 0, to me this feels like a cop-out and I want to be able to figure out how I could do this in the event that I ever need to. Maybe I will have a Rect class derive from point and then I will need to be able to check for collision with that!. I wouldnt just want to have that be derived from circle instead, that would be silly.


    edit: I added in the forward declaration in the location that MK27, however now I have errors that basically amount to the fact that since in the place where point uses circ, it uses member variables that it inherited from point, but i used "class circ;" without having it extend from point, it claims that those member variables are not a part of circ.
    Last edited by dcco; 06-17-2011 at 03:22 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    See post #6, I had to edit it. You can include all your includes in each other, don't worry.

    As to whether or not that is a good design decision, I'll defer to Salem -- but it's not impossible to do if you feel it's your best choice.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    8
    Okay, I still got the same error, but I when I tried it again using by only including circ.h in the cpp file and just using a forward declaration for the header file, I managed to get it to work. Thank you so much for helping me out guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Inclusion
    By Harsha.H in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 07:22 AM
  2. Odd error on header file inclusion
    By anatoly in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 12:20 PM
  3. preprocessor file inclusion?
    By dwaugh in forum C Programming
    Replies: 4
    Last Post: 08-22-2008, 09:01 AM
  4. Source file inclusion
    By sh3rpa in forum C++ Programming
    Replies: 7
    Last Post: 10-02-2007, 11:10 AM
  5. Failure in file inclusion
    By Mr_Miguel in forum C++ Programming
    Replies: 8
    Last Post: 06-06-2007, 02:49 PM