473,782 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5695
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.c om
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 "compilatio n 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
14576
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 possible to make doing multi-thread stuff? Thanks. YF
37
4898
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, Pujo
12
3880
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
6
4896
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 a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
4
17878
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 problem is the charset? How I can avoid this warning? But the worst thing isn't the warning, but that the program doesn't work! The program execute all other operations well, but it don't print the converted letters: for example, in the string...
5
3287
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 this? Private Sub cmdPreview_Click() On Error GoTo Err_Handler 'Purpose: Open the report filtered to the items selected in the list box. 'Author: Allen J Browne, 2004. http://allenbrowne.com Dim varItem As Variant 'Selected items
0
2328
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 Systems (MuCoCoS'08) Barcelona, Spain, March 4 - 7, 2008; in conjunction with CISIS'08. <http://www.par.univie.ac.at/~pllana/mucocos08> *********************************************************************** Context
1
9319
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
14
3414
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 libraries are suitable for multi-threaded programming. For people who are fond of portable C programming, what's the best way to go about multi-threaded programming? I've been reading up on POSIX threads a little, they seem pretty ubiquitous....
4
7329
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 vs. 0.3s for the single-dimensional array. (I know initializing is not necessary in this simple example,
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.