• Popular Now

• 13
• 27
• 9
• 9
• 20

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

Recommended Posts

Hey everyone, I was wondering how do you link files in Dev-C++ with different functions? Like say i make one file wiht the funtion "town ()" in it and i wannted to link it with the "main()" file how would i do that and do i have to declare the "town ()" function in the main file still?

Share on other sites
Declare the town function in a header file with the code in a cpp file.
Then include the include that town is in in your main source file.

Share on other sites
Firstly, you're going to need a project, and you're going to have to have all of your files in that project. Now, you write functions and later classes into any .cpp/.c file that you want to. So, you might have "town.cpp" that you define town() in.

Now, if you want to use a function or class that is defined in another file, you'll have to give a declaration of that function or class. So, in main.cpp you could put "int town();" and that would be the declaration of the function town(), if town returns an integer (if not, just put its return type there instead of the int). There's no need to link the function or town.cpp, just as long as you tell it that there is a town() function, it's fine.

However, this is tedious and bloats your files. Instead, you could make a .h file and put the declaration in that. So, town.h might have "int town();" in it. Then you could put "#include "town.h"" at the top of your main.cpp file (before you start main()).

Also, search around in the article section of this site, you'll find some nice articles about this. Hope that helps.

Thanks guys!

Share on other sites
Here an example

town.h
#ifndef TOWN_H_#define TOWN_H_void town();#endif

then in main.cpp
#include "town.h"//other includesint main(){    std::cout<<"in main...not in town.";}void town(){     std::cout<<"in town...";}

Hope that helps.

EDIT: TO LATE

Share on other sites
wouldnt it be easyer to make a genaric header file like this:
// towns.h// header file for townsvoid town_1 ();void town_2 ();void town_3 ();

then make the town-1.ccp,ect....
then the main file
// main.ccp#include <iosteam>#include "towns.h"using namespace std;int main (){town_1 {};}

Whould that work?

Share on other sites
Quote:
 Original post by lordcormwouldnt it be easyer to make a genaric header file like this:// towns.h// header file for townsvoid town_1 ();void town_2 ();void town_3 ();then make the town-1.ccp,ect....then the main file// main.ccp#include #include "towns.h"using namespace std;int main (){town_1 {};}Whould that work?

Yes, as long as you still give definitions for town_1(), town_2(), etc. in some .cpp file.

#ifndef HEADER_NAME_H#define HEADER_NAME_H// bla bla#endif

This will solve problems you will have with headers in the future.

Share on other sites
I know this is a little off topic, but along the same lines of his question. Is it possible to have one header file that includes all the functions you may use already declared. That way you can simply "#include functionsdeclared.h", and than simply define them in the .cpp files that call them?