shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
The error was because I never compiled the program. I just wrote in the Quick Reply pad and it looks like you got the point I was trying to explain which is the case with you also.
Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
But how can I declare the doubles x, y, and z so that they can be used in all the seperate files? When I include them in either header.cpp or header.h I'm not able to use them in main again, and when I include them in main, it says they're undefined in the other two files. If I were to include them all in each file, it creates an error having to do with multiple definitions of x, y, and z.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You can have them as global and assign them but you should not redeclare them in the function once again or else you will have the problem of the variable get assigned to the local one. Try seeing my both the function FuncLocal and FuncGlobal where I don't have variables declared in one of them and the behavior is as expected.
Lief Webster's Avatar, Join Date: Oct 2007
Go4Expert Member
So do I need to do anything different to use global variables over different files?
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You should not declare the variables once again in the function like SquareArea
dharmaraj.guru's Avatar
Go4Expert Member
Hi,

You can use extern keyword for it. If you have already declared some global variable in a .cpp file and want to access the same in another cpp file, better go for extern declarations. In the file where you want to access that variable, add the following statement.

You have declared a global variable in Header.CPP
Code:
 
// Header.CPP
int global_int;

// your code...
Add extern declarations in Other CPP files whereever you want to access.
Code:
// Main.CPP
extern int global_int;
||| Dharma |||