【发布时间】:2014-03-02 18:27:42
【问题描述】:
我想定义一个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()
^
总结问题:
- 似乎我无法在声明中使用比较来初始化集合,因为它依赖于 this。
- set 声明后的比较函数初始化失败,不知道为什么。
解决方案感谢@WhozCraig 将 mySet 声明为:
std::set<ContentType, doCompare> mySet;
并在初始化列表中将其初始化为:
mySet(doCompare(*this))
【问题讨论】:
-
可能普遍缺乏
const的正确性。 -
我用编译器错误更新了问题。该错误似乎与
doCompare定义中的consts 无关。我认为更基本的东西是错误的...... -
问题是,他想在构造函数的初始化列表中使用
this。另见:stackoverflow.com/questions/869281/… -
@Siamak 也许我不明白你的回答,或者你不明白我的问题。
std::set接受作为第二个模板参数的 type 用于比较(不是函数;虽然你可以使用函数类型,但你不是)。没有指定类型std::less<T>是默认值。您在构造函数中传递的比较器object(它不是一个函数,它是一个functor)与std::less<T>不匹配,也不匹配任何其他std::set<ContentType>备用构造函数参数列表。因此你的错误“没有匹配的功能”。 See it live -
再次查看发布的现场样本。回头看看我的第一条评论。该decl应该是
std::set<ContentType,doCompare>,注意第二个参数。没有this涉及(还没有;稍后在MyClass构造函数初始化列表中)。不相关(稍微),我相信您很有可能也应该将doCompare::_mc引用成员设为 const。我认为没有理由让它变热。
标签: c++ stl initialization set