A
Anonymous
How do I reference a namespace variable in a multi-file project? Do I
use the keyword 'extern'? If so, does the word 'extern' modify the
namespace or the individual variables within the namespace?
For example, suppose I have the following 3 files:
-----------------------//myns.h
#include <iostream>
using namespace std;
namespace n1 {
int a = 1;
namespace n2 {
int b = 2;
void myprint() { cout << b;}
}
}
----------------------//maincpp.cpp
#include "myns.h"
#include <iostream>
using namespace std;
extern void fc1();
extern int n1::a; //is this necessary?
extern int n1::n2::b; //likewise for this?
//extern namespace n1; //how about this?
//extern namespace n1::n2 //likewise?
int main () {
cout << n1::a;
cout << n1::n2::b;
fc1();
}
---------------------//othercpp.cpp
#include "myns.h"
#include <iostream>
using namespace std;
//what do I need here to allow the following function to work?
void fc1() {
cout << n1::a;
cout << n1::n2::b;
cout << n1::n2::myprint();
}
use the keyword 'extern'? If so, does the word 'extern' modify the
namespace or the individual variables within the namespace?
For example, suppose I have the following 3 files:
-----------------------//myns.h
#include <iostream>
using namespace std;
namespace n1 {
int a = 1;
namespace n2 {
int b = 2;
void myprint() { cout << b;}
}
}
----------------------//maincpp.cpp
#include "myns.h"
#include <iostream>
using namespace std;
extern void fc1();
extern int n1::a; //is this necessary?
extern int n1::n2::b; //likewise for this?
//extern namespace n1; //how about this?
//extern namespace n1::n2 //likewise?
int main () {
cout << n1::a;
cout << n1::n2::b;
fc1();
}
---------------------//othercpp.cpp
#include "myns.h"
#include <iostream>
using namespace std;
//what do I need here to allow the following function to work?
void fc1() {
cout << n1::a;
cout << n1::n2::b;
cout << n1::n2::myprint();
}