C++ Header Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

C++ Header Help

 
0
  #1
Jul 28th, 2008
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:

#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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: C++ Header Help

 
0
  #2
Jul 28th, 2008
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.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,037
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: C++ Header Help

 
0
  #3
Jul 28th, 2008
Some reading examples here, here and here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Re: C++ Header Help

 
0
  #4
Jul 28th, 2008
Originally Posted by CoolGamer48 View Post
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
  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
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,037
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: C++ Header Help

 
0
  #5
Jul 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Re: C++ Header Help

 
0
  #6
Jul 28th, 2008
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
  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
  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
  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: C++ Header Help

 
0
  #7
Jul 28th, 2008
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
  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

  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
  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

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.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 13
Reputation: DanDaMan is an unknown quantity at this point 
Solved Threads: 0
DanDaMan DanDaMan is offline Offline
Newbie Poster

Re: C++ Header Help

 
0
  #8
Jul 29th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: C++ Header Help

 
0
  #9
Jul 29th, 2008
Originally Posted by DanDaMan View Post
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
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,037
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: C++ Header Help

 
0
  #10
Jul 29th, 2008
Originally Posted by DanDaMan View Post
Now I'm really confused. When I don't include system("PAUSE"); then the console screen just pops up and instantly disappears.
Look here

Originally Posted by DanDaMan View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC