不明确的符号

Ambiguous symbol

本文关键字:符号 不明确      更新时间:2023-10-16

我在自定义命名空间中声明了一个类Integer:

namespace MyNameSpace
{
  class Integer {};
}

我用这样的方法:

void someMethod()
{
  using namespace MyNameSpace;
  SomeClass x(Integer("some text", 4));
}

这提供

10> error C2872: 'Integer' : ambiguous symbol
10>        could be 'g:libboostboost_1_47_0boost/concept_check.hpp(66) : boost::Integer'
10>        or       '[my file] : MyNameSpace::Integer'

我在我的代码库中搜索了全文搜索中的"namespace-boost"answers"usingboost",但没有找到像"usingnamespace-brust;"这样的行。的测试支持了这一点

void someMethod()
{
  shared_ptr<int> x;
  using namespace MyNameSpace;
  //SomeClass x(Integer("some text", 4));
}

给出

error C2065: 'shared_ptr' : undeclared identifier

void someMethod()
{
  boost::shared_ptr<int> x;
  using namespace MyNameSpace;
  //SomeClass x(Integer("some text", 4));
}

编译。

"模糊符号"错误发生的原因还有其他原因吗??

编译器只是防止您混淆这些类。即使您不使用命名空间"boost"。

命名空间本质上是其中内容的"姓氏"或"姓氏"。在您的例子中,Integer()的全名是MyNameSpace::Integer(。您的特定错误是使用名称空间的第一条规则的一个极好的例子。不要使用"USING"语句!如果你完全忽略了它们,是的,你必须键入一些额外的东西来安抚编译器。但你永远不会有碰撞,也不会问"boost在某个地方有整数吗"之类的问题。

其次,someMethod()在任何类和任何命名空间之外。它确实应该看起来更像MyNameSpace::Integer::someMethod(),或者更合理地位于内部

namespace MyNameSpace
{
   Integer::someMethod(
}

一旦你这样做,编译器将帮助你找到东西在哪里或不在哪里。

祝你好运!