Thread: Including problems...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    24

    Including problems...

    Hi, I have one simple(?) question about conflicts in including files. I use C++ Builder but actually I use it as C cause I know it better

    So, the cause of the problem is...

    --- map.h ---
    Code:
    #include "organism.h"
    
    struct Map {
       std::vector<Organism> guy_v;
    
      Map();
      ~Map();
    };
    --- organism.h ---
    Code:
    struct Organism {
       std::vector<Map>::iterator mapi;
    };
    So...organism.h is included first to define type Organism and then type Map is defined. So, Map needs to be defined/declared before Organism and Organism needs to be defined/declared before Map.

    What is the trivial way to include files without files depending on each others like that? Thx

    Maybe I could find some way alone but I think it would have problems that I didn't think of...oh and I know about externs
    Last edited by JulleH; 05-08-2006 at 03:00 AM.

  2. #2
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    merge two files, or make two types in one file.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    So, really, there's not any normal way to structure files of a program? I'd like to be able to include the types separately.

    Are there any good sites telling about this?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What you try to do is just not possible. Not even in a single file. Map needs to know the full declaration of Organism and Organism needs to know the full declaration of Map. In such a case forward declarations don't help either.
    Kurt

    EDIT: Tested it and it does work. I'm not shure why.
    just put a forward declaration of Map into organism.h

    Code:
    struct Map ;
    
    struct Organism {
        std::vector<Map>::iterator mapi;
    };
    Last edited by ZuK; 05-08-2006 at 12:20 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    just put a forward declaration of Map into organism.h
    Aah, yeah, I tried similar case earlier but it failed for me because I got an error "'guy_v' is not a member of Map, because the type is not yet defined" when I used watch to see the value of 'guy_v' or any other member.

    That sounds like little bit funny since I thought that Map IS defined anyway after compiling I can make a Map object and output values of members but couldn't for example permanently reserve() 'guy_v' vector in Map()...it worked when I did it after object was constructed
    Last edited by JulleH; 05-08-2006 at 02:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including extra .cpp files visual2008
    By Cathalo in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2009, 03:29 AM
  2. 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
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM