Multi-file project with Namespace

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();

}
 
R

Russell Hanneken

Anonymous said:
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
(e-mail address removed)
 
J

John Carson

Anonymous said:
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();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,812
Members
47,357
Latest member
sitele8746

Latest Threads

Top