943,895 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2587
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 28th, 2008
0

C++ Header Help

Expand Post »
Hey, I'm just starting to learn C++ and I need some help with a header file. I'm using a Dummies Guide book and it says that I need to create a header file with the extension .h. SO I created a source file called header.h and put this at the top of the main:

Quote ...
#include "header.h"
But when I compiled it I got an error message stating that all the functions and variables that were set in header.h were unknown to the main. So is there a problem with the way that I created/linked the header file?

This book really doesn't explain how to create header files, it just tells you to do so. And all the other C++ books I could find were outdated, with a copyright of 1993 (even older than me). I would really appreciate some help. Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Jul 28th, 2008
0

Re: C++ Header Help

Post the header file and the .cpp file. There's no specific special thing you need to do to create a header file. What you said could be correct, but it's a bit vague.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 28th, 2008
0

Re: C++ Header Help

Some reading examples here, here and here.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 28th, 2008
0

Re: C++ Header Help

Post the header file and the .cpp file. There's no specific
special thing you need to do to create a header file. What you said could be correct, but it's a bit vague.
main.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "header.h"
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7. variable = 20;
  8. function();
  9. system("PAUSE");
  10. return 0;
  11. }

header.h
C++ Syntax (Toggle Plain Text)
  1. extern int variable;
  2. void function();
Last edited by Ancient Dragon; Jul 28th, 2008 at 2:00 am. Reason: replace quote tags with code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Jul 28th, 2008
0

Re: C++ Header Help

You have prototyped the void function(); in the header file, but nowhere you have defined function(). How does the compiler know what it does?
Last edited by Aia; Jul 28th, 2008 at 1:46 am.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 28th, 2008
0

Re: C++ Header Help

Actually that's not the problem. The function was specified in another source file called proto.cpp. Anyway, I was playing around with the source files and somehow figured out how to make it work. But now there's a problem. I keep getting an error message and my console needs to close. Can you please help me out with this?

main.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "header.h"
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7. variable = 20;
  8. function();
  9. system("PAUSE");
  10. return 0;
  11. }

proto.cpp
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "header.h"
  4. using namespace std;
  5. int variable;
  6. void function()
  7. {
  8. cout << "Words" << endl;
  9. cout << endl;
  10. cout << "variable = " << variable << endl;
  11. }

header.h
C++ Syntax (Toggle Plain Text)
  1. using namespace std;
  2. extern int variable;
  3. void function();

When I run this I get the correct output:

Words

variable = 20


But then I get an error message that says that Project.exe (my file) has encountered a problem and needs to close. I sent the error message, but why is this happening and how can I prevent it?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Jul 28th, 2008
0

Re: C++ Header Help

Hi, i don't have a compiler in front of me.... but the program is small and easy so here is something that should work.

main.cpp
c++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2.  
  3. // #include <iostream>
  4. //don't include things that you don't use...
  5.  
  6. #include "header.h"
  7.  
  8. //using namespace std;
  9. //you don't use anything from std namespace
  10.  
  11. int main()
  12. {
  13. variable = 20;
  14. function();
  15. return 0;
  16. }

proto.cpp

c++ Syntax (Toggle Plain Text)
  1.  
  2. //#include <cstdlib> //you don't need cstdlib here...
  3.  
  4. #include <iostream>
  5. #include "header.h"
  6.  
  7. using namespace std;
  8.  
  9. int variable;
  10.  
  11. void function()
  12. {
  13. cout << "Words" << endl;
  14. cout << endl;
  15. cout << "variable = " << variable << endl;
  16. }

header.h
c++ Syntax (Toggle Plain Text)
  1. #ifndef HEADER_H
  2. #define HEADER_H
  3.  
  4. //using namespace std;
  5. // don't use using directives inside header files...
  6. // also here you don't need it anywhere....
  7.  
  8. extern int variable;
  9. void function();
  10.  
  11. #endif

Quote ...
When I run this I get the correct output:

Words

variable = 20

But then I get an error message that says that Project.exe (my file) has encountered a problem and needs to close. I sent the error message, but why is this happening and how can I prevent it?
you shouldn't get an error message.... the code seems correct...{the corrections i made, were minor...and the program shouldn't crash. Try using a debugger....

Also there is a convention when you have a cpp file like mycode.cpp the corresponding header file should be named: mycode.h..
Last edited by n.aggel; Jul 28th, 2008 at 10:20 am.
Reputation Points: 23
Solved Threads: 12
Posting Whiz in Training
n.aggel is offline Offline
202 posts
since Nov 2006
Jul 29th, 2008
0

Re: C++ Header Help

Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears. What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DanDaMan is offline Offline
13 posts
since Jul 2008
Jul 29th, 2008
0

Re: C++ Header Help

Click to Expand / Collapse  Quote originally posted by DanDaMan ...
Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears. What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?
Why should it (stay open)? Unless you ask for input, the program will do all the code you ask it to, outputting everything, and once it's done (either when it reachers return 0; or the end-bracket of main) it will close.

#define 'new symbol' 'previously known symbol' will make 'new symbol' and 'previously known symbol' look the same to the compiler.

#ifndef 'symbol' checks if 'symbol' is #defined. If so, it will execute all the code after it until it reached something that tells it to stop, like #endif
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Jul 29th, 2008
0

Re: C++ Header Help

Click to Expand / Collapse  Quote originally posted by DanDaMan ...
Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears.
Look here

Click to Expand / Collapse  Quote originally posted by DanDaMan ...
What are #ifndef and #define, and what's #endif? It works, thanks. But what are these three lines of code?
Those are called preprocessor directives.
Last edited by Aia; Jul 29th, 2008 at 10:23 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

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.
Message:
Previous Thread in C++ Forum Timeline: ifstream
Next Thread in C++ Forum Timeline: Code Speed Question





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


Follow us on Twitter


© 2011 DaniWeb® LLC