Thread: How do I properly #include?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Angry How do I properly #include?

    Can anyone point me to a detailed site to explain how to #include headers for a properly coded program? Or just explain it to me in this forum. I have four classes and a main function. The complier keeps telling me that it does not understand class names and because of that, it does not understand many different statements within my program.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    main.cpp (wherever Foo Class is needed)
    Code:
    #include "Foo.h"
    
    someFunction()
    {
        Foo bar();
        ...
    }
    Foo.h (Class Definition)
    Code:
    class Foo {...};
    Foo.cpp (Class Implementation - Headers needed for class go here too)
    Code:
    #include "Foo.h"
    
    Foo::Foo() {...}
    Last edited by major_small; 07-22-2005 at 06:36 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C++ Beginner
    Join Date
    Jun 2005
    Posts
    39
    Include is used like including a header to the project. Like
    [CODE]
    #include <iostream>
    #include <windows.h>
    #include <string>
    etc.
    [CODE]

    I like to include all of my headers in a header file called include.h like many people do.
    I'm a beginner C++ programmer, but I have studied HTML and Java. So if you need to help me I should catch on fast =)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It sounds like Yukumo is talking about your own headers, not library headers. And you should not put all includes in a file called include.h unless those includes are used almost everywhere in your program. Try to include each and every header you need, but no more.

  5. #5
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    Major Small - I've always just used a "classname.h" or used two seperate `.h`s if I wanted to separate the definition of the class and the definition of the functions in that class.
    Is that ok? I think I read something about a separate .h and .cpp in a book I have...

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    3

    Question More input needed

    Thank you, all of you. But I need more input. Take this code for example from my coplayer.h file.

    Code:
    #ifndef COPLAYER_H
    #define COPLAYER_H
    
    class CoPlayer
    {
    public:
    	CoPlayer(Card* recieveHand, SpadGame* pGame);
    	short playTurn(const Card cardsOnTable[]);
    	//*************************************************************************
    	// The spade variable in these search functions will be to tell the function 
    	// whether it includes the spade's suit in search for aces or not.
    	void findAce(bool spade);
    	void selectCard1();
    	//*************************************************************************
    	short chosenCardPos;
    private:
    	SpadGame* game;
    	const Card* hand;
    };
    
    #endif
    The compiler will not understand what "Card" and "SpadGame" are. I need to #include their class header files, but where shall I place the #include statements? Shall I put them at the top of the header file above the "#ifndef COPLAYER_H" statement? Or should I put it just below the "#define COPLAYER_H" statement? The only other option that I can think of would be to place the #include statements in the file coplayer.cpp. But if there, should they be placed above or below the "#include coplayer.h" statement?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    > Or should I put it just below the "#define COPLAYER_H" statement?

    Normally, this is the correct place to put your #includes.

    In this case, you only use pointers to these types in this header file, so you do not need to include the entire header file. Instead a forward declaration would work, and be better to avoid potential recursive includes and make compile times faster. A forward declaration would look like this:
    Code:
    #ifndef COPLAYER_H
    #define COPLAYER_H
    
    class Card;
    class SpadGame;
    
    class CoPlayer
    {
    You would then have to #include the headers in your coplayer.cpp file, since code in that file will probably use the Card and SpadGame classes.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    3
    Daved, your reply was wonderful. My compiler errors shrank to five. But I am hoping you or someone else can help me with my lesser problem now. Each place that I have a declaration of a Card class, the compiler tells me that the "Card" class is undefined. I'll use my deck class as an example.
    Code:
    #ifndef DECK_H
    #define DECK_H
    
    class Card;
    
    class Deck
    {
    public:
    	Deck();	// Constructor
    	~Deck();	// Deconstructor
    	void shuffleAndDeal();	// Function to shuffle and deal the appropiate amount of cards to each player
    	Card* getPtrOfPlayerHand(short);
    	void Organize();
    
    private:
    	Card playOneCards[13];	// Enough space for player #1's cards
    	Card playTwoCards[13];	// Enough space for player #2's cards
    	Card playThrCards[13];	// Enough space for player #3's cards
    	Card playFouCards[13];	// Enough space for player #4's cards
    };
    
    #endif
    Should I replace the forward declaration with [color = red]#include "card.h"[/color]? Or is there a more efficient way to correct this code? Again, your help was and is greatly appreciated.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Assuming that compiling your Deck.cpp is what is giving the undefined error and it is pointing at a line in Deck.h, then sure, go ahead and use the #include instead of the forward declaration. It is not a big deal, just a minor preference. I'm assuming that the arrays inside the class are what is causing the entire Card class declaration to need to be included.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Nazca
    Major Small - I've always just used a "classname.h" or used two seperate `.h`s if I wanted to separate the definition of the class and the definition of the functions in that class.
    Is that ok? I think I read something about a separate .h and .cpp in a book I have...
    it's perfectly fine, but generally, .h files should include definitions and .cpp files should include implementations.

    putting your entire class into a .h file is fine, but it voids the rules of encapsulation, or data hiding. what encapsulation means that, basically, I should only need to tell you this:
    Code:
    int foo(double in); //in is truncated and returned as an int
    the point is, you don't need to know how it does anything, just what it does.

    encapsulation is one of the fundamental concepts of Object-Oriented Programming.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    I have a general gist of encapsulation/need-to-know basis, but the idea is that the coder can look at the .h file and not worry about the .cpp file at all? Like a an unofficial standard.

    Good stuff. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use this part of dirent.h
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 01-31-2009, 08:51 AM
  2. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  3. Weird, cards not outputting correctly
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2007, 03:26 PM
  4. does not name a type ERROR
    By DarrenY in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2007, 04:54 AM
  5. #include problem
    By rwmarsh in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2006, 03:00 PM