namespace problem

V

vsgdp

I have a few variables that need to be global, but they are related and I
thought it be cleaner to put them in a namespace:

namespace x
{
A* a;
B b;
C c;
}

Several modules need access to this namespace, but I am getting a linker
error

"already defined in whatever.obj"

This is similar to what happens if I used plain globals, but that is easily
fixed by using extern and initializing the globals in one implementation
file. I tried to do something similar here but it didn't work. Any
solutions?
 
G

Gianni Mariani

vsgdp said:
I have a few variables that need to be global, but they are related and I
thought it be cleaner to put them in a namespace:

namespace x
{
A* a;
B b;
C c;
}

Several modules need access to this namespace, but I am getting a linker
error

"already defined in whatever.obj"

This is similar to what happens if I used plain globals, but that is easily
fixed by using extern and initializing the globals in one implementation
file. I tried to do something similar here but it didn't work. Any
solutions?

Try again - this below should work.

------- header file --------
namespace xxx
{
extern int a;
extern char foo[];
};
----------------------------

------- implementation cpp file -------
#include "header-file"

namespace xxx
{
int a = 2;
char foo[] = "ABCD";
};
---------------------------------------
 

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,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top