gpt4 book ai didi

c++ - 使用依赖于 "this"c++ 的比较结构初始化一个集合

转载 作者:行者123 更新时间:2023-11-30 04:07:57 25 4
gpt4 key购买 nike

我想定义一个 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];
}

};

这里 mySetMyClass 的成员,当我尝试使用初始化列表中的比较函数初始化集合时: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
  • 声明集合后用比较函数初始化失败,但我不知道为什么。

解决方案感谢@WhozCraig将 mySet 声明为:

std::set<ContentType, doCompare> mySet;

并在初始化列表中初始化为:

mySet(doCompare(*this))

最佳答案

您没有使用正确的模板比较器参数声明您的集合类型。这:

std::set<ContentType> mySet;

展开后的意思是:

std::set<ContentType, std::less<ContentType>> mySet

为简洁起见,省略了分配器。这意味着在构建 mySet 时并指定一个备用比较器仿函数,它必须是 std::less<ContentType> 类型,但你的不是。它的类型是 doCompare .编译器尝试匹配所有其他构造函数参数列表,但未能找到任何匹配项,最终导致您的错误。

更改您对 mySet 的声明到:

std::set<ContentType,doCompare> mySet;

现在类型应该正确连接。

正如我在评论中所说,我认为没有理由提及您的 MyClass被保存在你的比较器对象中应该是非常量的。除非你能想到一个很好的理由,否则我建议改为更改对 const 的引用,即 const MyClass&

关于c++ - 使用依赖于 "this"c++ 的比较结构初始化一个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22131559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com