|
-
March 2nd, 2008, 02:44 AM
#1
Declaring a global variable
Hello, I need to make a global variable for a number. I have a function that calculates this number. This function has its own .h and .cpp files.
In my main program .cpp file I have:
#include "findnumber.h"
double theglobalvar = findnumber();
int main(){ .... etc ....
My problem is I don't know how to use the function to declare my global variable to be the return value of the function.
It doesn't like me calling the findnumber function outside of main. The compiler says:
In function `__static_initialization_and_destruction_0(int, int)' roject_4.cpp .text+0xd4): undefined reference to `findnumber()'
collect2: ld returned 1 exit status
Any ideas?
Thanks
-
March 2nd, 2008, 02:47 AM
#2
Re: Declaring a global variable
Why do you need a global variable?
-
March 2nd, 2008, 02:48 AM
#3
Re: Declaring a global variable
 Originally Posted by spetsacdc
It doesn't like me calling the findnumber function outside of main. The compiler says:
That is not a compiler error. That is a linker error. The error is telling you that the "findnumber" function was called, but it doesn't exist anywhere.
So where is the "findnumber" function? Declaring it is not enough, it has to actually exist if you're going to call that function in your program.
Regards,
Paul McKenzie
-
March 2nd, 2008, 11:34 AM
#4
Re: Declaring a global variable
Oh ok sorry stupid mistake, I forgot to add findnumber.cpp to the end of my compile statement....
g++ -o program2 program2.cpp findnumber.cpp
Oh, and what does a makefile do? I think it is something to compile programs that have many different .h and .cpp source files, but I don't see why it is needed since that line above does it.
Thanks
-
March 2nd, 2008, 01:32 PM
#5
Re: Declaring a global variable
Take a look at the following link:
http://www.cs.bris.ac.uk/Teaching/Re...ake_intro.html
Regards
Doron Moraz
-
March 2nd, 2008, 01:54 PM
#6
Re: Declaring a global variable
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|