Problems with inclusions.

This is a discussion on Problems with inclusions. within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm running into a pretty consistent problem doing inclusions with header files. I suspect the problem is that ...

  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
    21,606
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 04: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, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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