Thread: Problems with inclusions.

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    16

    Problems with inclusions.

    Hi all,

    I'm running into a pretty consistent problem doing inclusions with header files. I suspect the problem is that I don't fully understand what inclusions do, but if someone could give me a hand figuring it out?

    Basically the problem is this. I have a header file and .cpp for a data type Planet, I also have a header file and .cpp for a data type Star. The header file for Star requires Planet to be included, as in it's constructer Star declares a vector of Planets.(I think this is the right way to do this?)

    The problem arises with #ifndef and #define. Everything complies fine if I use #include "planet.h", but when I switch to #ifndef, it tells me that star is not an object. If I use #include"planet.h" then it compiles again. This is the code I'm using at the moment.

    Code:
    #ifndef PLANET_H
    #define PLANET_H
    #include <iostream>
    #include <vector>
    #include "planet.h"
    Which compiles fine just so long as I leave the #include in there. But from what I was understanding, the #include shouldn't be necessary.

    Where it gets worse, is for my main program I tell it...

    Code:
    #ifndef STAR_H
    #define STAR_H
    #ifndef PLANET_H
    #define PLANET_H
    And no matter what it tells me that star is

    "error C2065: 'star' : undeclared identifier"

    Even when I put in #include for star and planet.

    Could anyone point me to what I'm doing wrong? If it helps any my header files...

    planet- includes no .h files
    star- includes planet.h
    main- inludes star.h and planet.h

    planet.cpp- includes only planet.h
    star.cpp- includes only star.h

    Any help is appriciated, I've tried reordering things till my minds gone kinda wonky...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    But from what I was understanding, the #include shouldn't be necessary.
    It is necessary, since planet.h is the file you want to include. Header guards are not a substitue for #include.

    Your header files might be something like this:

    Code:
    // for planet.h
    #ifndef PLANET_H
    #define PLANET_H
    
    // Planet related code
    
    #endif
    Code:
    // for star.h
    #ifndef STAR_H
    #define STAR_H
    
    #include "planet.h"
    
    // Star related code
    
    #endif
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    You only put the 'include guards' for a header around that header itself, NOT inside the files that will be including the header.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    16
    Ah!

    Thank you! That explains it, Ty both

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM