1,105,416 Community Members

How to write a header file

Member Avatar
Saleh
Newbie Poster
5 posts since May 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
4
 

where can i find out a detailed information about How to write a header file ?

Member Avatar
Bleek
Light Poster
28 posts since Mar 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
3
 

A header file is made up of pre-processor directives, classes, namespaces etc. a good place to look at is cplusplus.com. look at the classes, objects and namespaces chapters...

Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
1
 

My God I even had a comp science teacher ask me this.Arrrg one will go mad.

It's simple.Cut a few funtions out of you code and paste them in a file with a .h extension and call #include "your_header_name.h" to include that.

Note: not < > but " " if it's in the current directory else you have to supply the full or relative path.

All the rules are same as normal C++ syntax.

----------------------------------------------
That was very simple thing.Headers can be used for more things.They can just contain funtions declarations without definitions.In that case it will be used to link to a static library.

Eg.Make a header file with 2 funtion declarations like fun1(); fun2(); only
Include it with a cpp file and call the funtions and compile.

You will get no errors on compile but on linking you will see an error similar to the following:

undefined module fun1() refferenced from Module main.cpp
undefined module fun2() refferenced from Module main.cpp

I hope you understand a header in not really diffrent from a cpp file.Btw you can also include .cpp files.

Eg: #include "wow.cpp"

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It is very very simple:


write some functions||define some macros||define structures/classes etc...
DO NOT include a main() function
save it with the name you want
e.g header.txt/cpp/in/out/...
you use it through: #include "..."
if you have variables in your header, do not redeclare them in your programs or it will give you an error "Multiple declaration for variable..."
:p

Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

Even if you include main() in a header and dont declare main() in any other funtion the program will complie and work properly.Headers are just a way to organise code.

Member Avatar
Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

well if you include main() in a header that would be more like a program than like a header but anyway you can ;)

Member Avatar
sadds
Newbie Poster
2 posts since Apr 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-5
 

did the computer science teacher ask you it on a test?

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

did the computer science teacher ask you it on a test?

You bumped a 6-year-old thread for that????

Member Avatar
sdsda
Newbie Poster
5 posts since Apr 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

bump

Member Avatar
JCRARY
Newbie Poster
2 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

where can i find out a detailed information about How to write a header file ?

Trying to learn c++. The tutorial is missing a .h (header file) ?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article