Results 1 to 12 of 12
Thread: Header Help
-
July 26th, 2008, 11:29 PM #1
Header Help
Hey, I'm just starting to learn C++ and I'm using a Dummies Guide book. It says that I need to make a header file and use #include "header.h" for the linking. So I created a new source file called header.h, but when I compile it I get an error saying that there's a Linker error and that there's an undefined reference to all the variables that are set in the header file. Is this because I'm not making the header file correctly? Help is appreciated.
Slinky + Escalator = Everlasting Fun!
-
July 26th, 2008, 11:40 PM #2
Could you post your source code please? It's kind of hard to tell what's going wrong without seeing code. However, you probably are doing something wrong in your header file.
-
July 27th, 2008, 12:03 AM #3
-
July 27th, 2008, 01:18 AM #4
Code
main.cpp
proto.cpp#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#include <cstdlib>
#include "header.h"
#include <iostream>
using namespace std;
void funtio() {
cout << "Some random words." << endl;
cout << variable << endl;
}
extern int variable;
void function();Slinky + Escalator = Everlasting Fun!
-
July 27th, 2008, 03:53 AM #5117Registered User
posts
-
July 27th, 2008, 11:26 AM #6
Whoops. Thanks but I still get the same error message. Why?
Slinky + Escalator = Everlasting Fun!
-
July 27th, 2008, 06:57 PM #7
You are not including the proto.cpp file anywhere. Change it to proto.h, move it to the headers folder (assuming you are using VS) and include it in your main.cpp file.
-
July 27th, 2008, 09:09 PM #8
But, proto.cpp is fine. Everything is working the way it should be. Except for the fact that I made a source file called header.h and for some reason main.cpp doesn't recognize anything that was set in header.h. I'm using Dev-C++ 4.9.9.2.
Slinky + Escalator = Everlasting Fun!
-
July 28th, 2008, 03:01 AM #9117Registered User
posts
-
July 28th, 2008, 03:41 AM #10
Yeah, I did. I started playing around with it and somehow I fixed it. I have no idea what I did lol. Thanks for your help, everyone
Slinky + Escalator = Everlasting Fun!
-
August 11th, 2008, 09:03 PM #11158Code Monkey
postsNot getting answers? Read this!
-
August 26th, 2008, 10:27 PM #12
it seems like your defining the variable 'variable' in the main function block, it should be global for the extern to be able to see it. Just initiate it outside of the main function block. You should also declare the variable in every seperate file that it occurs in, which means also in the proto.cpp file.
Main.cpp:
proto.cpp:Code:#include <cstdlib> #include <iostream> #include "header.h" using namespace std; // move variable outside main block function and decalre it again since its in a seperate file int variable = 20; int main(int argc, char *argv[]) { function(); system("PAUSE"); return 0; }
havent tested it but it looks okay to me.Code:#include <cstdlib> #include "header.h" #include <iostream> // also declare variable here as extern because it is used in this file extern int variable; using namespace std; void funtion() { cout << "Some random words." << endl; cout << variable << endl; }
Last edited by dozza92; August 27th, 2008 at 09:32 PM.

Reply With Quote



Bookmarks