Click to See Complete Forum and Search --> : Make your own .h file
sql_lall
Mar 5th, 2002, 04:33 AM
I know this has probably been asked many times, but here we go again anyway:
I have a function i created that takes in two variables, prints stuff out, then returns another integer:
int printit(int* days, int first)
{
....
return 0;
}
and i was wondering if i could make my own .h file, so that i could use the function without having to write it out for each source.
i.e. up the top of some code have:
#include <iostream.h>
#include <myownh.h>
Also, can i have a constant array defined as well??
If so, could someone post some example code?? Thankyou! :D
CornedBee
Mar 5th, 2002, 07:16 AM
Creating a header is easy. Write a text file and save it with a .h extension.
A header only contains function prototypes, so in your case:
int printit(int* days, int first);
add this file to every project where you want to use it and write
#include "myheader.h"
You also must add the source file or object file where the function code is inside to the project.
What do you mean with "constant array"?
sql_lall
Mar 7th, 2002, 04:01 AM
Thanks for your help. :)
So you're saying i can't actually have a function in a .h file that i don't have to include the source to each time a make a new program??? See, was i was wanting to do was make a compilation of my most-used functions, store them in a .h file so i didn't have to write them out each time.
Also, constant array in this case means an array that won't be edited. (Just has 31 values that stay the same.)
CornedBee
Mar 7th, 2002, 05:31 AM
const type name[size];
You can write one seperate source file and a fitting header file. Then include both files in every project you need them.
To save compilation time you can create an object file:
Right-click on the source file and choose compile. A name.cpp file is then compiled to the file name.obj. Simply include this file instead of the source file in the projects you need your functions, then they don't need to be compiled over and over.
To reduce the size of your executable, create a Win32 Static Library project and choose your source file as the only file of the project. Then build the project and you'll get a .lib file: a static library. When you need the functions, simply add the file name to the list in the linker settings (the file must be in a known directory). The linker will only include the functions you actually use.
All versions require a header file.
The last method would be a dll, but I won't explain it because it's more complicated and in your case (a small collection fo some often-used functions) the contras are bigger than the pros.
sql_lall
Mar 10th, 2002, 02:37 AM
I've figured it out. All i have to do is make a class, and have one of the member functions the function i want to use.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.