Nov 17, 2012 at 7:24pm UTC
I'm using Code::Blocks and the GNU compiler, and I have a multi-file program. I have multiple functions in different files using the same variables. How do I make a global variable, or work around this?
Nov 17, 2012 at 7:32pm UTC
Last edited on Nov 17, 2012 at 7:33pm UTC
Nov 17, 2012 at 7:53pm UTC
Yes, this is an excellent idea. But in case I need to test something, how do you make a global variable in a Multi-File project.
Nov 17, 2012 at 8:00pm UTC
You will need to use the "extern" keyword in every file that uses that global variable.
http://msdn.microsoft.com/en-us/library/0603949d(v=vs.80).aspx
main.cpp:
1 2 3 4 5 6 7 8 9
#include <iostream>
#include "test.h"
int global = 2;
int main(void ) {
foo();
std::cout << global << '\n' ;
}
test.h:
1 2 3 4 5
extern int global;
void foo(void ) {
global+=2;
}
Last edited on Nov 17, 2012 at 8:03pm UTC
Nov 21, 2012 at 1:47am UTC
Okay, what if I need to return a string?
1 2 3 4 5 6 7 8
void getName(){
string name;
cin << name;
return name;
//How can I do this?
}
Last edited on Nov 21, 2012 at 1:48am UTC
Nov 21, 2012 at 9:51pm UTC
oh, you can do that? Interesting, I will investigate. (New to C++)