我想定义一个基于当前类其他成员的值进行比较的set
:
std::set<ContentType> mySet(doComparison(*this));
其中doCompare是一个结构:
struct doCompare{
doCompare( MyClass& mc ) : _mc(mc) { }
MyClass& _mc;
bool operator()( const ContentType & i1, const ContentType & i2 ){
return _mc.otherMember[i1] < _mc.otherMemeber[i2];
}
};
此处mySet
是MyClass
的成员,当我尝试使用初始化列表中的比较函数初始化集合时:mySet(doCompare(*this))
代码无法编译。
我在这里做错了什么?
错误是:
no matching function for call to ``std::set<ContentType>::set(MyClass::doCompare)`
以下是完整的消息(为了更好的可读性,更改了名称):
./myclass.h:74:165: error: no matching function for call to ‘std::set<ContentType>::set(MyClass::doCompare)’
: mySet(doCompare(*this)) {
^
./myclass.h:74:165: note: candidates are:
In file included from /usr/include/c++/4.8/set:61:0,
from ./myclass.h:12,
from [blah blah]
/usr/include/c++/4.8/bits/stl_set.h:193:7: note: std::set<_Key, _Compare, _Alloc>::set(const std::set<_Key, _Compare, _Alloc>&) [with _Key = ContentType; _Compare = std::less<ContentType >; _Alloc = std::allocator<ContentType >]
set(const set& __x)
^
/usr/include/c++/4.8/bits/stl_set.h:193:7: note: no known conversion for argument 1 from ‘MyClass::doCompare’ to ‘const std::set<ContentType >&’
/usr/include/c++/4.8/bits/stl_set.h:180:2: note: template<class _InputIterator> std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator, const _Compare&, const allocator_type&)
set(_InputIterator __first, _InputIterator __last,
^
/usr/include/c++/4.8/bits/stl_set.h:180:2: note: template argument deduction/substitution failed:
In file included from [blahblah]:
./myclass.h:74:165: note: candidate expects 4 arguments, 1 provided
: mySet(doCompare(*this)) {
^
In file included from /usr/include/c++/4.8/set:61:0,
from ./myclass.h:12,
/usr/include/c++/4.8/bits/stl_set.h:163:2: note: template<class _InputIterator> std::set<_Key, _Compare, _Alloc>::set(_InputIterator, _InputIterator)
set(_InputIterator __first, _InputIterator __last)
^
/usr/include/c++/4.8/bits/stl_set.h:163:2: note: template argument deduction/substitution failed:
In file included from [blahblah]:
./myclass.h:74:165: note: candidate expects 2 arguments, 1 provided
: mySet(doCompare(*this)) {
^
In file included from /usr/include/c++/4.8/set:61:0,
from ./myclass.h:12,
from blahblah:
/usr/include/c++/4.8/bits/stl_set.h:148:7: note: std::set<_Key, _Compare, _Alloc>::set(const _Compare&, const allocator_type&) [with _Key = ContentType; _Compare = std::less<ContentType >; _Alloc = std::allocator<ContentType >; std::set<_Key, _Compare, _Alloc>::allocator_type = std::allocator<ContentType >]
set(const _Compare& __comp,
^
/usr/include/c++/4.8/bits/stl_set.h:148:7: note: no known conversion for argument 1 from ‘MyClass::doCompare’ to ‘const std::less<ContentType >&’
/usr/include/c++/4.8/bits/stl_set.h:139:7: note: std::set<_Key, _Compare, _Alloc>::set() [with _Key = ContentType; _Compare = std::less<ContentType>; _Alloc = std::allocator<ContentType >]
set()
^
总结一下这个问题:
解决方案感谢@WhozCraig 将mySet声明为:
std::set<ContentType, doCompare> mySet;
并在初始化列表中将其初始化为:
mySet(doCompare(*this))
答案 0 :(得分:2)
您没有使用正确的模板比较器参数声明您的设置类型。这样:
std::set<ContentType> mySet;
展开时表示这一点:
std::set<ContentType, std::less<ContentType>> mySet
为了简洁而省略了分配器。这意味着在构建mySet
并指定备用比较器仿函数时,它必须是std::less<ContentType>
类型,但不是。{1}}。它的类型为doCompare
。编译器尝试匹配所有其他构造函数参数列表,无法找到任何匹配项,最终导致您的错误。
将mySet
的声明更改为:
std::set<ContentType,doCompare> mySet;
现在类型应该正确连接。
正如我在评论中所说,我认为你的比较器对象中保存的MyClass
的引用不应该是非const的。除非你能想出一个很好的理由,否则我建议改为改为对const的引用,即const MyClass&