小编rc4*_*559的帖子

具有相同参数的命名空间中的函数和函数之间的歧义

任何人都可以解释为什么A::f(const B& b)和之间存在歧义f(const A::B& b)。我认为代码对意图非常明确。

#include <iostream>

namespace A
{
  class B
  {
  protected:
    double value_;
  public:
    B() : value_(15.0) {}
    double getValue() const {return value_;}
  };

  void f(const B& b)
  {
    std::cout << "f(b) = " << b.getValue() << std::endl;
  }
}

void f(const A::B& b)
{
  std::cout << "Other f(b) = " << b.getValue() << std::endl;
}

int main()
{
  A::B b;
  A::f(b);
  f(b);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,error: call of overloaded ‘f(A::B&)’ is …

c++ namespaces function-call argument-dependent-lookup

14
推荐指数
2
解决办法
702
查看次数