gpt4 book ai didi

c++ - 无法使用客户比较仿函数构造 std::map 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 01:42:29 26 4
gpt4 key购买 nike

#include <map>
using namespace std;

class C {
public:
C(map<int,int> m) { }
int operator()(int a, int b) {
return a < b;
}
};

int main() {
map<int, int> m;
map<int, int, C> mymap(C(m));
mymap.insert(pair<int,int>(1,1));
}

为什么会出现以下错误?:

main.cpp: In function 'int main()':
main.cpp:16:11: error: request for member 'insert' in 'mymap', which is of non-class type 'std::map<int, int, C>(C)'
mymap.insert(pair<int,int>(1,1));

这是 coliru 链接:http://coliru.stacked-crooked.com/a/0413a35d3177ef48

最佳答案

这是一个令人烦恼的解析 - 函数声明示例,您会在其中期望一个对象。

试试这个:

map<int, int, C> mymap((C(m)));

关于c++ - 无法使用客户比较仿函数构造 std::map 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26780007/

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