2012-12-22 74 views
4

当我编写C++代码时,我尝试使用using <X>防止污染太多。在Crypto ++中,它在一种情况下给我带来问题。这种情况是CryptoPP命名空间中的ASN1命名空间(它只显示在一个地方)。使用嵌套命名空间声明“using namespace”时出错(“namespace xxx :: yyy不允许在使用声明中”)

以下是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 
+1

'使用命名空间CryptoPP :: ASN1;' –

回答

12

只是说:

using namespace CryptoPP::ASN1; 
+0

谢谢@Charles。这导致'ASN1 :: secp256r1()'错误:'ASN1'未被声明。 – jww

+3

直接调用'secp256r1()'而不使用命名空间限定符 –

4

ASN1是一个命名空间。尝试:

using namespace CryptoPP::ASN1; 
+0

谢谢@Silpertan。这导致'ASN1 :: secp256r1()'错误:'ASN1'未被声明。 – jww

2

尝试

using CryptoPP::ASN1::secp256r1; 

...然后调用secp256r没有资格。这避免了使用名字空间,有些皱眉头。

1

其他的答案建议更换using namespace CryptoPP::ASN1;但是这不是你想要的(大概),因为它进口所有的的ASN1命名空间的内容到你的范围。

我的猜测是你要做到这一点:

namespace ASN1 = CryptoPP::ASN1; 

这将让你在你的范围内使用,例如ASN1::secp256r1()

相关问题