accessing parent namespace in c++
I am using two 3rd party frameworks. I want to access a new class from both frameworks.
The first framework uses a nested namespace like:
namespace space1 {
namespace space2 {
class 3rdparty{
}
}
}
the second framework doesn't use any namespaces. If I try to call my own class from the first framework that uses the namespace I simply can't get it right. In best case I end up with linking errors.
My Class looks like this:
PlugIn_Processor.h:
class PlugIn_Processor{
public:
//constructor
PlugIn_Processor();
};
PlugIn_Processor.cpp:
#include "PlugIn_Processor.h"
PlugIn_Processor::PlugIn_Processor(){
};
The cpp file which uses the first framework looks like this:
namespace space1 {
namespace space2 {
3rdparty::3rdparty{
PlugIn_Processor * plu开发者_C百科gIn_Processor;
plugIn_Processor = new PlugIn_Processor();
}
}
}
However, whatever I do, I end up with linker errors when calling the constructor. A solution would be to put everything under the name space of the 3rd party framework, but as I want to access that class from different frameworks, I don't want to put everything under that namespace. What could I do to get around this? Everything I tried just failed.
To access a parent scope, use a double colon (::) as in:
class Blah;
namespace alpha
{
class Blah;
}
namespace bravo
{
class Blah;
// can access outer class as ::Blah
// and one from alpha as ::alpha::Blah
}
Note, though, that in your example the following code should not compile:
3rdparty::3rdparty{
Is this actually a snippet of code? And did it really compile for you? Also, be sure to check that you are actually linking against the compilation unit in which the referenced class was defined.
精彩评论