Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you extern a namespace scope variable?

Status
Not open for further replies.

CodingNovice

Programmer
Dec 30, 2003
108
0
0
GB
I have been using C++ lately rather than C. In C we have no namespaces so defining a global variable in one translation unit and declaring it as extern in another is fine. I know I can also do that in C++ but what I am trying to do instead is to have a namespace scoped variable extern'ed. Can it be done?

I tried this but it gave errors....

file1.h
namespace NS
{
class A {};
}

global.cpp
#include "file1.h"
namespace NS
{
A object;
}

file1.cpp
#include "file1.h"
extern NS::A NS::eek:bject;
int main()
{
// use object
return 0;
}

Its pretty obvious what I am trying to achieve. How do I do this?
 
If you change this line :
extern NS::A NS::eek:bject;


to this :
extern NS::A object;

you should be in business ....

/JOlesen
 
And that gives a linker error instead of a compilation error!
 
A.h

#ifndef nsdemo
#define nsdemo

namespace NS
{
class A
{
public:
int a;
};

}
#endif

globals.cpp

#include "A.h"

namespace NS
{
A obj;
}

main.cpp

#include "A.h"
#include <iostream>

using namespace std;

extern NS::A obj;

int main()
{
obj.a = 10;
cout << obj.a<<endl;
cin.get();
return 0;
}

I get these two errors....

namespace demo error LNK2001: unresolved external symbol &quot;class NS::A obj&quot; (?obj@@3VA@NS@@A)
namespace demo fatal error LNK1120: 1 unresolved externals

Fully patched up version of Visual C.net Academic. So whats wrong here?
 
You forget
Code:
using namespace NS;
directive, for example:
Code:
#include &quot;file1.h&quot;
using namespace NS;
...
A a; // and so on
Or:
Code:
#include &quot;file1.h&quot;
NS::A a; // A from NS...
That's all. Marked as extern names come from the global :: namespace...
 

>>>Marked as extern names come from the global :: namespace..

Does that mean I cannot extern a namespace scoped variable. Adding using directive made no difference, still get a linker error as above. If I remove the namespace the extern works wonderfully but if I try to wrap my globals in a namespace then I cant seem to extern them.
Im sure it must be able to be done. Aren't cout and cin externed from within namespace std?
 
If you change this line :
extern NS::A obj;

to this :
NS::A obj;

It works ok.

I also did build the first version - where extern is used -and can't tell why you have to remove it in this version ... but I am working on it ...

/JOlesen
 
If you change this line :
extern NS::A obj;

to this :
NS::A obj;

It works ok.


Have you considdered why? That's a global variable definition not declaration as the extern was. I want to put all &quot;global&quot; variables in a namespace(s) and extern them throughout my code as in such a way as the &quot;globals&quot; need not be in the same translation unit. I just cant seem to do this. Is it possible? Any language lawyers in?
 
Arrrgh ... so simple....

I made your first example work because there wasn't any real reference to the external object. As soon as I actually use (or try to ..) the object, the linker fails (It does however allow you to print the size of the object).

I don't understand why you want global variables in a namespace.
If you remove all this namespace stuff you have no problems ?

/JOlesen
 
Why should they be global if they could be namespaced?
Allows reuse of names but different namespaces etc.
Just wanted to know if it was possible and if not why not?
Im really just playing with the language at the moment seeing what can and what can not be done. I haven't got a copy of the standard to check as it is not available for free.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top