473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Multi-file project with Namespace

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
2 5669
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: dkelly925 | last post by:
Is there a way to add an If Statement to the following code so if data in a field equals "x" it will launch one report and if it equals "y" it would open another report. Anyone know how to modify...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
As far as I know, the C Standard has no mention of multi-threaded programming; it has no mention of how to achieve multi-threaded programming, nor does it mention whether the language or its...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.