【问题标题】:Error declaring "using namespace" with a nested namespace ("namespace xxx::yyy not allowed in using-declaration")使用嵌套命名空间声明“使用命名空间”时出错(“使用声明中不允许命名空间 xxx::yyy”)
【发布时间】:2012-12-22 02:14:39
【问题描述】:

当我编写 C++ 代码时,我尝试使用 using <X> 来避免过多的污染。在 Crypto++ 中,它在一种情况下给我带来了问题。案例是 CryptoPP 命名空间中的 ASN1 命名空间(它只出现在一个地方)。

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

例如,我可以使用 secp256r1 曲线:

CryptoPP::ASN1::secp256r1();

但是,我还没有想出一种使用 using 来声明它的方法。当我尝试时:

#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

【问题讨论】:

  • using namespace CryptoPP::ASN1;

标签: c++ namespaces


【解决方案1】:

只要说:

using namespace CryptoPP::ASN1;

【讨论】:

  • 谢谢@Charles。这导致error: ‘ASN1’ has not been declaredASN1::secp256r1()
  • 不带命名空间限定符直接调用secp256r1()
【解决方案2】:

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

我猜你想这样做:

namespace ASN1 = CryptoPP::ASN1;

这将允许您在您的范围内使用例如ASN1::secp256r1()

【讨论】:

    【解决方案3】:

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

    using namespace CryptoPP::ASN1;
    

    【讨论】:

    • 谢谢@Silpertan。这导致error: ‘ASN1’ has not been declaredASN1::secp256r1()
    【解决方案4】:

    试试

    using CryptoPP::ASN1::secp256r1;
    

    ...然后无条件地调用secp256r。这避免了使用命名空间,有些人对此并不满意。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      相关资源
      最近更新 更多