user682765
user682765

Reputation: 1229

conflicting class names in namespace

I have a class which is not part of any namespace

class A(*) .

And I have another class with same name but part of namespace

class A part of namespace B.

In xyz.cpp, I have the below:

#include "..."

using namespace B;
// some code
A::var; // This A should be part of (*) and not namespace B.
// some code

But since I have conflicting class names, I get errors. Is there a way to get around this?

Upvotes: 0

Views: 323

Answers (2)

yanchong
yanchong

Reputation: 376

You may not use

using namespace B;

but use like

 B::A::var 

instead.

Upvotes: 0

kfsone
kfsone

Reputation: 24249

The using namespace keyword imports all of the names from the specified namespace into the global namespace. Since you already declared a class A in the global namespace, this results in a conflict.

Solution: Don't use using namespace B.

This is effectively what you're doing:

namespace GLOBAL {
    class A { ... };
};

namespace B {
    class A { ... };
};

using namespace B /* export 'B::A' into 'GLOBAL' resulting in a conflict; */ ;

Upvotes: 1

Related Questions