error with 2 header files calling each other

This is a discussion on error with 2 header files calling each other within the C++ Programming forums, part of the General Programming Boards category; i get error when i compile.. here is my code Code: /*this is my header file for my file called ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    91

    error with 2 header files calling each other

    i get error when i compile..
    here is my code
    Code:
    /*this is my header file for my file called C.h*/
    #ifndef _C_H_
    #define _C_H_
    #include "I.h"
    using namespace std;
    class C {
     public:
        void get_foo(int, int);
        void set_bar(int,I*);
        void monkey(int, I*);
        void cow(I*);
     private:
        I * stinky; 
    };
    #endif
    Code:
    /*This is my header file for my file called I.h*/
    #ifndef _I_H_
    #define _I_H_
    #include "C.h"
    using namespace std;
    class I{
     public:
          void get_monkey(int, C *);
     private:
          C * hello;
    };
    #endif
    Now when i compile my program i get errors relating to all the lines that contain the type of other header file.
    so in the I.h i get errors on all the lines that have the type C.
    and in the C.h i get errors on all the lines that have the type I.

    Code:
    In file included from I.h:3,
                     from I.cpp:2:
    C.h:31: error: type specifier omitted for parameter `Item'
    C.h:31: error: syntax error before `*' token
    C.h:33: error: type specifier omitted for parameter `Item'
    C.h:33: error: syntax error before `*' token
    C.h:34: error: `Item' was not declared in this scope
    C.h:34: error: syntax error before `)' token
    C.h:49: error: syntax error before `*' token
    make: *** [I.o] Error 1
    
    /*ignore the line numbers because i have removed all the irrelevant functions for this example*/
    but here line 31 is referring to
       void set_bar(int,I*);
    from the C.h file.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    try this: instead of putting

    #include <I.h>

    in the C.h file, put

    class H;

    before the declaration of class C. Since I.h hasn't been compiled yet, it's inappropriate to #include it in C.h. I think the line class H; in C.h is called something like a forward declaration, though I can't readily find it in the reference I have with me.

    Edit: you may also need to do the same in I.h --- delete #include "C.h" and place class C; before the declaration of class I;
    Last edited by elad; 10-05-2004 at 09:02 AM.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    120
    Elad, that is a forward declaration, and it is the correct way to go about things. You can forward declare I in C.h and then include C.h in I.h, or simply forward declare I and C in the right files.

    Don't forget to include I.h in C.c and C.h in I.c! If you don't, it won't work because the compiler still needs to know how the whole class is laid out.

    And I think forward declarations won't work for templates because the implementation is in the header file (thus the compiler needs to know where the fields and methods are). Could be wrong on this one...haven't written a template class in awhile.

    PK

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    91
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21