使用嵌套命名空间声明“使用命名空间”时出错(“在声明中不允许使用命名空间xxx :: yyy”)

时间:2012-12-22 02:14:39

标签: c++ namespaces

当我编写C ++代码时,我尝试使用using <X>来防止污染太多。在Crypto ++中,它在一个案例中给我提问。案例是CryptoPP命名空间中的ASN1名称空间(它只显示在一个地方)。

这是Crypto ++中的声明:http://www.cryptopp.com/docs/ref/oids_8h_source.html

我可以使用secp256r1曲线:

CryptoPP::ASN1::secp256r1();

但是,我还没有找到一种方法来使用它来声明它。当我尝试:

#include <cryptopp/asn.h>
#include <cryptopp/oids.h>
using CryptoPP::ASN1;

最终会导致error: namespace ‘CryptoPP::ASN1’ not allowed in using-declaration,然后是error: ‘ASN1’ has not been declared以下(我尝试过两者):

ECIES<ECP>::Decryptor d1(prng, secp256r1());
ECIES<ECP>::Decryptor d2(prng, ASN1::secp256r1());

如果有多个命名空间,如何使用using语句?


$ g++ -version
i686-apple-darwin11-llvm-g++-4.2

4 个答案:

答案 0 :(得分:16)

请说:

using namespace CryptoPP::ASN1;

答案 1 :(得分:4)

ASN1是名称空间。尝试:

using namespace CryptoPP::ASN1;

答案 2 :(得分:4)

其他答案建议Test,但这不是您想要的(大概),因为它将所有 using namespace CryptoPP::ASN1;命名空间的内容导入您的范围。

我的猜测是你想要这样做:

ASN1

这样您就可以在范围内使用namespace ASN1 = CryptoPP::ASN1;

答案 3 :(得分:2)

尝试

using CryptoPP::ASN1::secp256r1;

...然后无资格地调用secp256r。这避免了使用命名空间,有些人不赞成。