Using multiple files

This is a discussion on Using multiple files within the C++ Programming forums, part of the General Programming Boards category; I have a question about a problem that I've been having that gets in the way of development. Say I ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    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
    591
    Bad idea from me....delete delete delete
    Last edited by Barjor; 05-20-2002 at 02:33 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288
    how do i use one class or function from another file?
    -Save the whales. Collect the whole set.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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, 05:35 AM
  2. need assistance with multiple files
    By c++.prog.newbie in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2006, 12:44 AM
  3. Windows shell commands - multiple files
    By Magos in forum Tech Board
    Replies: 3
    Last Post: 02-28-2006, 12: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

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