Novice question - Splitting up Code?
I know it is good practice to split up your code into header and source files, but if I have three different source files, how do I connect them all? Does including the header file also include the source file?
You don''t need to #include source files, just use em.
It is good practice to split up your code into different files. Find out what works best for you though.
It is good practice to split up your code into different files. Find out what works best for you though.
If you''re using any IDE (eg Visual C++ or CodeWarrior) just make sure all of your files are in one project and they should compile and link fine. If not you''re going to have to read your linker''s manual on how to link all of the compiled files together.
--------------------------Insert witty comment here!--------------------------
If you're not using a project, you do have to include the source file. But if you're using something like VC++, you're always in a project, in which case you just have all your headers and source files in the project and compile. Make sure your source files include their corrisponding headers. If you want to call any of the functions (or use the classes, however you program it) that are in the source files, include the header file in the file you want to call from. You generally put class declarations and function prototypes in headers and all the actual implementation (the code) in source files. So for example, if you're using a project, you might have something like this.
So yeah, you just put all of those 3 files in the same project and it should work. Your compiler will normally just ignore the header files and compile all your source files, which need the header files included in order, in this example, know what the walruss class is. If you're not in a project, on the other hand, then at the bottom of the header file (before the #endif), #include the cpp file (#include "walruss.cpp"), and get rid of including the header file from the source. This way in your main source file (the one with your main function) you just include the header file, which includes the source file, and you're all set. Hope this helps, and good luck to the walrusses.
- f l u c k y p o o
- the geek inside
[edited by - flucknugget on May 12, 2002 11:24:53 PM]
// this is WALRUSS.H#ifndef _WALRUSS_H#define _WALRUSS_Hclass walruss{public: walruss(); void sex(); void change_mate(walruss *new_mate);private: int color; int size; walruss *mate;};#endif// this is WALRUSS.CPP#include "walruss.h"#include <stdlib.h>walruss::walruss(){ color = rand()%16; size = rand()%300+200; mate = NULL;}void walruss::sex(){ /* insert walruss sex code here */}void walruss::change_mate(walruss *new_mate){ mate = new_mate;}// this is MAIN.CPP#include "walruss.h"#include <iostream.h>void main(){ walruss jack; walruss jone; jack.change_mate(&jone); jack.sex(); cout << "a wonderful thing has just happened.\n";}
So yeah, you just put all of those 3 files in the same project and it should work. Your compiler will normally just ignore the header files and compile all your source files, which need the header files included in order, in this example, know what the walruss class is. If you're not in a project, on the other hand, then at the bottom of the header file (before the #endif), #include the cpp file (#include "walruss.cpp"), and get rid of including the header file from the source. This way in your main source file (the one with your main function) you just include the header file, which includes the source file, and you're all set. Hope this helps, and good luck to the walrusses.
- f l u c k y p o o
- the geek inside
[edited by - flucknugget on May 12, 2002 11:24:53 PM]
- f l u c k y p o o
I''m not gonna ask where you came up with that code.
WNDCLASSEX Reality;......Reality.lpfnWndProc=ComputerGames;......RegisterClassEx(&Reality);Unable to register Reality...what's wrong?---------Dan Uptonhttp://0to1.orghttp://www20.brinkster.com/draqza
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement