Jump to content
  • Advertisement
Sign in to follow this  
ekrax

the old multiple source files ...

This topic is 5082 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

well for the longest time ive been using .h files have the declaration and the defination and never really botherd with multiple source files ... but i guess thats stupid so now im trying to use multiple source files and i cant get anything to work, ive read a bunch of articles on this forum but it still isnt clear. here is an example ... Common.h _______________________________ #include <iostream> #include <string> #include <vector> typedef unsigned char byte; typedef unsigned int uint; extern Car; // ??? _______________________________ Car.h _______________________________ #include "Common.h" class Car { private: uint fuel; uint oil; public: void fillFuel(uint); void fillOil(uint); uint condition(); Car(); }; _______________________________ Main.cpp _______________________________ #include "Common.h" int main() { Car honda(); return 0; } _______________________________ Car.cpp _______________________________ #include "Common.h" #include "Car.h" void Car::fillFuel(uint x) { fuel += x; if (fuel > 100) fuel = 100; } void Car::fillOil(uint x) { oil += x; if (oil > 100) oil = 100; } uint Car::condition() { return (fuel + oil) / 2; } Car::Car() { fuel = 100; oil = 100; damage = 0; } _______________________________ this is what im trying just to try and get the multiple source files working but it doesnt work ... so my question(s) is why doesnt this work? how do i use a common header and get around recompiling compiled code? should using multiple source files follow this format: common.h main.cpp main.h other.cpp other.h things.cpp things.h ... where every .h file contains common.h ??? im lost on this. thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Use inclusion guards. They'll make sure that common.h is only included once by the preprocessor, no matter how many times it would otherwise be included. If you're not sure how they work, check out this site:

http://www.devx.com/tips/Tip/12738

Also i'm not sure why you're using the extern to get the Car class available in main.cpp. If you #include "Car.h" in main.cpp, that'll give you access to your Car class there.

Hope this helps

LSD

Share this post


Link to post
Share on other sites
you'd probably want something like this :
car.h includes common.h
car.cpp includes car.h
main.cpp includes car.h
main.cpp includes common.h (if you need it)

however, its generally considered a bad idea to have a "globals.h" or "commons.h" with a bunch of system headers - most of your classes probably won't need them (as is obvious with your program).
<edit :: cheers, AP>

Share this post


Link to post
Share on other sites
#include "car.h"

If you include car.h in your main.cpp and removed the extern car from common.h that should give you the functionality you want.

You also might want to wrap your .h files with include guards to prevent including a file multiple times:

for example car.h becomes:

#ifndef CAR_H
#define CAR_H

#include "Common.h"

class Car {
private:
uint fuel;
uint oil;
public:
void fillFuel(uint);
void fillOil(uint);
uint condition();
Car();
};

#endif

Similarly, common.h would use COMMON_H etc.

andrew.

Share this post


Link to post
Share on other sites
ok im convering my larger project now into multiple files and have ran into a problem, from everyones above posts i think i can solve it with the ifndef ... etc but im not sure why there is a problem in the first place ... ie

Main.h:
#include <windows.h>
#include // GL headers
//
//
#include "WinGL.h"

Main.cpp:
#include "Main.h"

ok now

WinGL.h:
// include nothing since windows.h and gl headers are declared
// above it

WinGL.cpp:
#include "WinGL.h"

now WinGL.cpp doesnt seem to have access to any windows or gl functions / types etc ... why?! Main.h includes windows and gl headers ... then Wingl.h is includes after. so why doesnt winGL have access to windows and gl?

Share this post


Link to post
Share on other sites
Quote:

Main.h:
#include <windows.h>
#include // GL headers
//
//
#include "WinGL.h"

Main.cpp:
#include "Main.h"

ok now

This should be fine for learning how this all works.

Quote:

WinGL.h:
// include nothing since windows.h and gl headers are declared
// above it


The headers are declared above it in main.cpp, but in wingl.cpp they arnt which causes your problem.

wingl.h is a seperate file and does not include anything. You need to include the windows.h gl.h, etc whatever else you need in this file.

Also you should declare your custom header files first, this will help identify when they dont have everything they need.

[Edited by - Hylo on October 24, 2004 2:54:01 PM]

Share this post


Link to post
Share on other sites
yay i got it all working except now my window gives me a billion errors probably cause i messed up something else not related to what i was doing. anyways it compiles and links, just a few questions about the style in which im doing things.

ok so i have 10 headers and 10 cpp files. each cpp only includes its header file and every header file has "inclusion" guards on it and they include whatever other header file they need. however there is no rythem or rhyme to the order of declaration and definition, doest it really matter? or is there a certain standard on how these things should be done?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!