Dave Mateer Dave Mateer - 1 year ago 218
C++ Question

C++ namespace alias and forward declaration

I am using a C++ third party library that places all of its classes in a versioned namespace, let's call it

. They also define a generic namespace alias:

namespace tplib = tplib_v44;

If a forward-declare a member of the library in my own .h file using the generic namespace...

namespace tplib { class SomeClassInTpLib; }

... I get compiler errors on the header in the third-party library (which is being included later in my .cpp implementation file):

error C2386: 'tplib' : a symbol with this name already exists in the current scope

If I use the version-specific namespace, then everything works fine, but then ... what's the point? What's the best way to deal with this?

[EDIT] FYI for future viewers, this was the ICU library. A solution (at least in my situation) is in the comments to the accepted answer.

Answer Source

It looks like there is an ugly workaround for this, but no good solution.

For ACE (with a decent explanation) and Xerces (with a snarky "this is how c++ works" comment), they define macros that you can use to do this "generically".

class ACE_Reactor;

class DOMDocument;
class DOMElement;

It looks like an unfortunate c++ artifact, try searching around in your tplib for these macros.

The standard treats namespaces and namespace aliases as different things. You're declaring tplib as a namespace, so when the compiler tries to assign an alias later, it cannot be both, so the compiler complains.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download