Including objects from another file

This is a discussion on Including objects from another file within the C++ Programming forums, part of the General Programming Boards category; I'm not exactly sure on how to do this, or if it's possible. I have a main.cpp which is the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    26

    Including objects from another file

    I'm not exactly sure on how to do this, or if it's possible.

    I have a main.cpp which is the main loop. I then have a battle.cpp & .h which defines a class called Character.
    And lastly I have an enemy.cpp which takes the Character class from battle.h and creates some Character objects eg. Character Person etc.

    My question is how can I include the Character objects made by enemy.cpp, in main.cpp?
    Last edited by HLMetroid; 01-27-2008 at 02:59 AM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    What do you mean by "creates some Character objects"?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by King Mir View Post
    What do you mean by "creates some Character objects"?
    Character c;
    OR
    Character* pC = new Character;

    --------------

    Anyway, the best way would be to pass those objects by reference or pointer the the functions requiring it to avoid unnecessary global variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Yeah, if Character, Person etc. are global variables, then you want to change that; it's bad design.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Thanks for the help so far.

    Ok, I originally had this:

    Main.cpp:
    Code:
    /* main.cpp */
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include "battle.h"
    #include "enemy.h"
    
    using namespace std;
    
    int battle(Character, Character);
    
    Character Enemy1("Goblin",50,2,2);            // enemy character
    Character Person("Normal Person",100,10,10);  // hero
    
    int main()
    {
        battle(Person,Enemy1);
        return 0;
    }
    Battle.h
    Code:
    /* battle.h */
    
    #ifndef BATTLE_H_INCLUDED
    #define BATTLE_H_INCLUDED
    
    class Character
    {
    public:
              Character(char*,int,int,int);  //constructor
    int       get_hp(Character);
    void      hit(Character&);        
    void      enemyhit(Character&);
    void      display(Character,Character);
    
    
    private:
    char      *name;
    int       hp;
    int       attack;
    int       defence;
    };
    Now I wanted to have an enemy.cpp file that did this:

    Code:
    /* enemy.cpp */
    
    #include "enemy.h"
    #include "battle.h"
    
    Character Enemy2("Goblin2",60,4,4);     // A new enemy
    And what I wanted to happen was for the Enemy2 character in enemy.cpp to get passed to main.cpp
    so I could easily add enemies and stuff, and they could then be used in the main battle loop.

    However I did the pointer thing with the Character class, but still can't get it passed to main.cpp.


    Code:
    /* enemy.cpp */
    
    #include "enemy.h"
    #include "battle.h"
    
    Character* Enemy2 = new Character("Goblin2",50,2,2);     // A new enemy
    Is that right? Should I be doing something different to be able to pass the Enemy2 character to my battle loop?
    Last edited by HLMetroid; 01-27-2008 at 03:08 PM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Avoid global variables. Let your main function create all those enemies. The advantage of that is that they will be all in the same source file.
    All functions needing access to the Enemy can take a Character& or const Character& argument.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    26
    Thanks for the help, I'll do what you suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Including a Pascal File Written in Delphi
    By jamez05 in forum C++ Programming
    Replies: 0
    Last Post: 09-23-2005, 03:18 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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