943,815 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 72537
  • C++ RSS
May 25th, 2004
4

How to write a header file

Expand Post »
where can i find out a detailed information about How to write a header file ?
Reputation Points: 14
Solved Threads: 0
Newbie Poster
Saleh is offline Offline
5 posts
since May 2004
May 25th, 2004
3

Re: How to write a header file

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...
Reputation Points: 45
Solved Threads: 0
Light Poster
Bleek is offline Offline
28 posts
since Mar 2004
May 27th, 2004
1

Re: How to write a header file

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"
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 17th, 2004
0

Re: How to write a header file

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
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Jun 18th, 2004
0

Re: How to write a header file

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.
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Jun 18th, 2004
0

Re: How to write a header file

well if you include main() in a header that would be more like a program than like a header but anyway you can
Reputation Points: 16
Solved Threads: 0
Light Poster
Fili is offline Offline
34 posts
since Jun 2004
Apr 26th, 2010
-5
Re: How to write a header file
did the computer science teacher ask you it on a test?
Reputation Points: 9
Solved Threads: 0
Newbie Poster
sadds is offline Offline
2 posts
since Apr 2010
Apr 26th, 2010
0
Re: How to write a header file
Click to Expand / Collapse  Quote originally posted by sadds ...
did the computer science teacher ask you it on a test?
You bumped a 6-year-old thread for that????
Last edited by Fbody; Apr 26th, 2010 at 10:49 am.
Featured Poster
Reputation Points: 833
Solved Threads: 392
Posting Maven
Fbody is offline Offline
2,846 posts
since Oct 2009
Apr 26th, 2010
-2
Re: How to write a header file
bump
Reputation Points: 7
Solved Threads: 0
Newbie Poster
sdsda is offline Offline
5 posts
since Apr 2010
Jul 24th, 2010
0
Re: How to write a header file
Click to Expand / Collapse  Quote originally posted by Saleh ...
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) ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
JCRARY is offline Offline
2 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: What Am I Doing Wrong???????
Next Thread in C++ Forum Timeline: Suddenly error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC