Results 1 to 7 of 7

Thread: #include

  1. #1

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238

    #include

    I have a header file, Classes.h, and another header file, Functions.h.
    Theres two classes in Classes.h, and one function in Functions.h. Class A needs to pass a reference to class B to the function in Functions.h. Now classes.h needs to include functions.h so it knows recognizes the function name, but functions.h needs to include classes.h because it needs to recognize the class that is being passed to it. But if you include functions.h in classes.h and classes.h in functions.h, you get an infinite recursion error. I have tried including classes.h and function.h in the main cpp file, but that doesn't work. how can i solve this?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Put header guarding in both .h files. Don't put any code into those files, they only contain either template functions, or prototypes. You might need a Functions.cpp file as well.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    Whats header gaurding?

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Header gaurding looks like this:
    Code:
    #ifndef INCLUDED_MY_PROJECT_CLASSES_H
    #define INCLUDED_MY_PROJECT_CLASSES_H
    
    // Classes.h code here...
    
    #endif
    The first time the header is included INCLUDED_MY_PROJECT_CLASSES_H will not yet be defined, so the code gets compiled. If the header is included a second time, INCLUDED_MY_PROJECT_CLASSES_H is already defined, so everything between the #ifndef and the #endif is ignored.

    Also, make sure that each header defines a different name.

  5. #5

    Thread Starter
    Addicted Member jmiller's Avatar
    Join Date
    Jul 2002
    Location
    University of Michigan
    Posts
    238
    I still can't get it to work. I've done this:
    Main.cpp:
    #include "Classes.h";
    Classes.h (header gaurded):
    #include "Functions.h";
    Functions.h (not header gaurded):
    #include "Classes.h";

    I don't get the recursion error, but Functions doesn't recognize anything in Classes, and vice versa. Please tell me what i'm doing wrong.
    thanks

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Functions.h needs the code from Classes.h, but that code comes after the #include of Funcions.h.
    The problem is that Classes.h needs Functions.h, and Functions.h needs Classes.h. The only solution for this problem is that you change the design of your code files. If for example Classes.h only needs some part of Functions.h, which in turn does not need Classes.h, you could put that part of the code in a separate file, BasicFunctions.h:
    Code:
    BasicFunctions.h
      // Some functions
    Classes.h
      #include "BasicFunctions.h"
      // Some classes
    Functions.h
      #include "Classes.h"
      // Some functions
    It's also posible that you put function implementations in your header files. You shouldn't do that, implementations should go into .cpp files. Put only declarations in headers.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    If you only need a reference in the functions.h header you can add a forward declaration for the class:

    class B;

    void func(B &rb);
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width