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)':project_4.cpp:(.text+0xd4): undefined reference to `findnumber()'
collect2: ld returned 1 exit status
Any ideas?
Thanks
Re: Declaring a global variable
Why do you need a global variable?
Re: Declaring a global variable
Quote:
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
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
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
Re: Declaring a global variable