Thread: Using multiple files

  1. #1

    Using multiple files

    I have a question about a problem that I've been having that gets in the way of development. Say I have file a.h, and file b.h.

    File b.h contains:

    Code:
    class x {
     public:
      y obj_y;
    };
    and file a.h contains:

    Code:
    class y {
     public:
      x obj_x;
    };
    #include-ing them to each other causes an #include loop. Is there any way around this?

    Thanks for the help,
    Valar_King
    -Save the whales. Collect the whole set.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    Bad idea from me....delete delete delete
    Last edited by Barjor; 05-20-2002 at 02:33 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One of them will have to contain a pointer to the other, otherwise the data structure expands recursively.

    In one or two files, you can do this
    Code:
    class y;  // a forward declaration
    
    class x {
     public:
      y *obj_y;
    };
    
    class y {
     public:
      x obj_x;
    };

  4. #4
    how do i use one class or function from another file?
    -Save the whales. Collect the whole set.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Like this?

    File b.h contains:
    Code:
    class y;  // a forward declaration
    
    class x {
     public:
      y *obj_y;
    };
    File a.h contains:
    Code:
    #include "b.h"
    class y {
     public:
      x obj_x;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. need assistance with multiple files
    By c++.prog.newbie in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2006, 01:44 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 01:56 AM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 09:57 PM