| | |
C++ Header Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
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:
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.
•
•
•
•
#include "header.h"
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.
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include "header.h" using namespace std; int main(int argc, char *argv[]) { variable = 20; function(); system("PAUSE"); return 0; }
header.h
C++ Syntax (Toggle Plain Text)
extern int variable; void function();
Last edited by Ancient Dragon; Jul 28th, 2008 at 2:00 am. Reason: replace quote tags with code tags
•
•
Join Date: Jul 2008
Posts: 13
Reputation:
Solved Threads: 0
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
proto.cpp
header.h
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?
main.cpp
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include "header.h" using namespace std; int main(int argc, char *argv[]) { variable = 20; function(); system("PAUSE"); return 0; }
proto.cpp
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> #include "header.h" using namespace std; int variable; void function() { cout << "Words" << endl; cout << endl; cout << "variable = " << variable << endl; }
header.h
C++ Syntax (Toggle Plain Text)
using namespace std; extern int variable; 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?
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
proto.cpp
header.h
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..
main.cpp
c++ Syntax (Toggle Plain Text)
#include <cstdlib> // #include <iostream> //don't include things that you don't use... #include "header.h" //using namespace std; //you don't use anything from std namespace int main() { variable = 20; function(); return 0; }
proto.cpp
c++ Syntax (Toggle Plain Text)
//#include <cstdlib> //you don't need cstdlib here... #include <iostream> #include "header.h" using namespace std; int variable; void function() { cout << "Words" << endl; cout << endl; cout << "variable = " << variable << endl; }
header.h
c++ Syntax (Toggle Plain Text)
#ifndef HEADER_H #define HEADER_H //using namespace std; // don't use using directives inside header files... // also here you don't need it anywhere.... extern int variable; void function(); #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?
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"
by Robert Frost the "The Road Not Taken"
•
•
•
•
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?
#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...".
•
•
•
•
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?
Last edited by Aia; Jul 29th, 2008 at 10:23 pm.
![]() |
Similar Threads
- PHP-Nuke Tutorial #1: Altering the Header Tags (PHP)
- Html header of a page changed programmatically? (ASP.NET)
- New header (DaniWeb Community Feedback)
- How to write a header file (C++)
- c vs h header files... (C)
- missing function header (C++)
Other Threads in the C++ Forum
- Previous Thread: ifstream
- Next Thread: Code Speed Question
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






