By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,284 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Multi-file project with Namespace

P: n/a
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();

}
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Anonymous wrote:
How do I reference a namespace variable in a multi-file project? Do I
use the keyword 'extern'?
Yes.
If so, does the word 'extern' modify the
namespace or the individual variables within the namespace?
The variables.

For example [ . . . ]

extern int n1::a; //is this necessary?
extern int n1::n2::b; //likewise for this?


You would think that would work, but (if I recall correctly) it won't.
I believe it's necessary to do something like this:

namespace n1
{
extern int a;

namespace n2
{
extern int b;
}
}

Hope that helps,

Russell Hanneken
rh*******@pobox.com

Jul 19 '05 #2

P: n/a
"Anonymous" <no@spam.net> wrote in message
news:of********************************@4ax.com
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();
myprint includes its own cout, so you have two couts. Moreover, myprint
returns void, which cout cannot do anything with.


}

You seem to be trying to learn this stuff without access to a compiler to
try it. This is a very difficult task.

You have a more fundamental problem than namespaces. Each variable and each
function is only allowed to be defined once. By #including myns.h in two
files, myns.h becomes a part of two "compilation units" and hence your
variables and function are defined twice. This will give a linker error.
(The only cases in which you should define functions in header files are
when they are inline or template functions.)

There are several ways to proceed. One good practice is to declare
everything in a .h file and define it in a .cpp file. The .h file variable
declarations must be made extern (this is not necessary with the function
declarations).
-----------------------//myns.h

namespace n1
{
extern int a; // declare only, don't initialise

namespace n2
{
extern int b; // declare only, don't initialise
void myprint(); // declare only, don't define
}

}

-----------------------//myns.cpp

#include <iostream>
using namespace std;
#include "myns.h"

namespace n1
{
int a = 1;

namespace n2
{
int b = 2;
void myprint() { cout << b;}
}

}
Now you just #include myns.h in both maincpp.cpp and othercpp.cpp and no
other declarations are necessary other than

void fc1();

You could alternatively move void fc1(); into othercpp.h and then #include
othercpp.h.
----------------------//maincpp.cpp
#include "myns.h"
#include <iostream>
using namespace std;
void fc1();

int main () {

cout << n1::a;
cout << n1::n2::b;

fc1();

}

---------------------//othercpp.cpp

#include "myns.h"
#include <iostream>
using namespace std;
void fc1() {

cout << n1::a;
cout << n1::n2::b;
n1::n2::myprint();

}

--

John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.