| View previous topic :: View next topic |
| Author |
Message |
Estraik Expert Cheater
Reputation: 0
Joined: 05 Feb 2008 Posts: 128
|
Posted: Thu Oct 09, 2008 6:42 am Post subject: Help with header file |
|
|
How do I start a header file in C++?
I want to know how to show it on the source code and how to call it.
Also would like to know what to include in the .h file:
Like, does it goes like the source code?
By that I mean:
If the header file need to have an:
int main() ?
Or if something else, what would it be? and how to end the header file?
And how to call it inside the source code?
Any help would be appreciated.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Oct 09, 2008 7:15 am Post subject: |
|
|
mything.h: | Code: | #ifndef _MYTHINGH
#define _MYTHINGH
#include <stdio.h>
// Functions, structures, classes, etc.
void StupidFunction();
#endif |
mything.c: | Code: | // The actual functions.
void StupidFunction() {
printf("Hello World!\n");
} |
main.c: | Code: | #include "mything.h"
int main(int argc, char *argv[])
{
StupidFunction();
return 0;
} |
EDIT: Added lil' more code (main.c).
EDIT: More more more.
Last edited by Jani on Thu Oct 09, 2008 7:23 am; edited 3 times in total |
|
| Back to top |
|
 |
Zerith Master Cheater
Reputation: 1
Joined: 07 Oct 2007 Posts: 468
|
Posted: Thu Oct 09, 2008 7:17 am Post subject: |
|
|
The header file usually contains function definitions, class definitions and so forth.
You can add a header file in MSVC++ clicking Add item -> Header file(.h).
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Thu Oct 09, 2008 9:19 am Post subject: |
|
|
Just so you know, the header file could contain anything another file contains.
So you could write your whole program in the headerfile if you wanted to, but that's not the purpose of it.
You could also write programs without header files. Most of my programs are small and have no header files, and only a 'main.cpp'.
|
|
| Back to top |
|
 |
_TheChosen_ Advanced Cheater
Reputation: 0
Joined: 13 Jul 2007 Posts: 50 Location: Oaxaca
|
Posted: Thu Oct 09, 2008 7:29 pm Post subject: |
|
|
A header files main purpose is re-usable code. Think everyone else explained everything else.
_________________
Ciento dos huevos. |
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Thu Oct 09, 2008 11:47 pm Post subject: |
|
|
Header file should contain the code, .cpp file should contain all the declarations and constants, like this:
| Code: |
//foobar.c
#define MAX_POOP 10
#define ERROR_MESSAGE "You has an error!"
#include foobar.h
|
| Code: |
//foobar.h
int main()
{
//do stuff
}
|
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Fri Oct 10, 2008 2:10 am Post subject: |
|
|
Are you being sarcastic? (hope so)
It's exactly the other way around. The reason you put constants and stuff in the header file, is because when you include it in all your projects files and then want to change the constant, you only have to change it once in the header file.
|
|
| Back to top |
|
 |
nog_lorp Grandmaster Cheater
Reputation: 0
Joined: 26 Feb 2006 Posts: 743
|
Posted: Tue Oct 14, 2008 12:16 am Post subject: |
|
|
You CAN do it my way though! You can even do
_________________
Mutilated lips give a kiss on the wrist of the worm-like tips of tentacles expanding in my mind
I'm fine accepting only fresh brine you can get another drop of this yeah you wish |
|
| Back to top |
|
 |
|