如何解决这个编译错误?编译日志说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/