c++ - 为什么在传递列表作为参数时模棱两可?

标签 c++

如何解决这个编译错误?编译日志说caller和candidate完全一样,但是有重载和歧义? 代码:

Ctrl.h
namespace CvRcgCtrllr {
    bool AssignPidsTo(const list<unsigned int> & pids, CvRcg & rcg);
    bool RemovePidsFrom(const list<unsigned int> & pids, CvRcg & rcg);
};

Ctrl.cpp
using namespace CvRcgCtrllr;
         30 bool AssignPidsTo(const list<unsigned int> & pids, Rcg & rcg)
         31 {     
         44     return true;
         45 }
         46
         47 bool RemovePidsFrom(const list<unsigned int> & pids, Rcg & rcg)
         48 {
         49     
         50     //Rcg default_rcg = GetNewRcg("default");
         51     //bool res = AssignPidsTo(pids, default_rcg);
         52     return res;
         53 }

<!-- -->

CvRcgCtrllr.cpp: In function ‘bool RemovePidsFrom(const std::list<unsigned int, std::allocator<unsigned int> >&, Rcg&)’:
CvRcgCtrllr.cpp:51: error: call of overloaded ‘AssignPidsTo(const std::list<unsigned int, std::allocator<unsigned int> >&, Rcg&)’ is ambiguous
CvRcgCtrllr.cpp:30: note: candidates are: bool AssignPidsTo(const std::list<unsigned int, std::allocator<unsigned int> >&, Rcg&)
CvRcgCtrllr.h:20: note:                 bool CvRcgCtrllr::AssignPidsTo(const std::list<unsigned int, std::allocator<unsigned int> >&, Rcg&)

最佳答案

你不能仅仅通过做来定义命名空间成员

using namespace CvRcgCtrllr;

然后在没有范围解析运算符的情况下指定成员。它并不像您认为的那样起作用。在您的代码中,您在 CvRcgCtrlr 中声明了一对函数,然后在 global 命名空间中另外定义了一对完全独立的函数。这就是在重载解析期间导致歧义的原因。

为了从 .cpp 文件中的 CvRcgCtrlr 命名空间定义函数,您必须重新打开命名空间

namespace CvRcgCtrllr
{
  bool AssignPidsTo(const list<unsigned int> & pids, Rcg & rcg)
  {
    // Whatever
  }
}

或者使用限定的函数名

bool CvRcgCtrllr::AssignPidsTo(const list<unsigned int> & pids, Rcg & rcg)
{
  // Whatever
}

没有办法避免这个或那个。 using namespace CvRcgCtrlr; 在这里对您没有帮助。

关于c++ - 为什么在传递列表作为参数时模棱两可?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309410/

相关文章:

c++ - C++中 `std::sort`比较器的不同类型

c++ - 从友元函数访问静态成员函数

c++ - Kruskal 的算法实现

c++ - 计算文本 OpenCV 的偏斜

c++ - 在类构造函数中使用vector时发生运行时错误?

时间:2019-03-17 标签:c++: no execution performance for STL parallel algorithm

c++ - 我们是否应该创建具有固定大小的 vector ,然后在需要更多或不初始化时 push_back

关于 ABC、继承和虚函数的 C++ 赋值,试图找出段错误等

c++ - 在中心而不是在一个顶点合并颜色顶点?

c++ - char* 在从 C DLL 返回到 js-ctypes 时丢失