Including code from another .cpp file

This is a discussion on Including code from another .cpp file within the C++ Programming forums, part of the General Programming Boards category; How would I go about including code from another .cpp file into the one I'm working on? Example: An RPG ...

  1. #1
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Including code from another .cpp file

    How would I go about including code from another .cpp file into the one I'm working on?

    Example: An RPG file called game.cpp has all the actual coding to control your character and the levels. A separate file called enemies.cpp has the information with all the monsters in the game. Another file called battle.cpp has the coding for the battle stage when you encounter the monster.

    I want to include the other .cpp files almost like functions to be more orderly and to make things easier to edit. The simple #include enemis.cpp wouldn't work, would it, because there would be two main() functions?

    Help is greatly appreciated.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Your enemies.cpp and battle.cpp shouldn't have a main() in them. They could contain defenitions to functions or classes pertaining to enemies and battle respectively. The forward declarations to those functions and classes are best put in a header file such as enemis.h and battle.h. You can then include those header file in your game.cpp to be able to use the functions and classes defined in the cpp files.

  3. #3
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33

    Hmm...

    Alright, thank you.

    Edit: I made a program with just an int main() in it and include <class3.h> (my include file).

    I took the H file directly from a program I had already compiled, and the compiler is saying there is "no such file or directory". I use MSVisualC++ Toolkit 2003. What's the problem?
    Last edited by nickodonnell; 09-30-2005 at 09:35 AM. Reason: compile

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by nickodonnell
    Alright, thank you.

    Edit: I made a program with just an int main() in it and include <class3.h> (my include file).

    I took the H file directly from a program I had already compiled, and the compiler is saying there is "no such file or directory". I use MSVisualC++ Toolkit 2003. What's the problem?
    Is the header in your working directory? or did you include the full path of where to find it in your #include?

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    It should be
    Code:
    #include "class3.h"
    Kurt

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by ZuK
    It should be
    Code:
    #include "class3.h"
    Kurt
    Only if he copied it to the working directory of his current project

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    It should be game.cpp includes battle.h, and enemies.h.
    Then battle.cpp includes battle.h, and enemies.cpp includes enemies.h.
    And finally game.cpp, battle.cpp, and enemies.cpp should all be part of your Project List.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered Loser nickodonnell's Avatar
    Join Date
    Sep 2005
    Location
    United States
    Posts
    33
    Yeah, it's in the directory. Thanks, I'll try it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions in .C file not recognized in .CPP file
    By tooKool4School6 in forum C++ Programming
    Replies: 1
    Last Post: 06-02-2006, 10:30 AM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Including multiple .cpp file in project
    By bonkey in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2002, 07:41 AM

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