gpt4 book ai didi

c++ - 如何遍历这样的 map ?如何修复 8 个可怕的 C2784 错误?

转载 作者:行者123 更新时间:2023-11-28 08:18:47 25 4
gpt4 key购买 nike

所以我尝试编译这样的代码:

bool server_utils::find_service_by_name_iterator_function(std::pair<boost::shared_ptr<service>, server_utils::service_description> const & element, std::string name) const
{
return element.second.name == name;
}

server_utils::service_description server_utils::stop_service_by_name(std::string name)
{
typedef std::map<boost::shared_ptr<service>, server_utils::service_description> map_t;
map_t::iterator map_it = std::find_if(description.service_map.begin(), description.service_map.end(), std::bind1st(std::ptr_fun(&server_utils::find_service_by_name_iterator_function), name));

if (map_it != description.service_map.end())
{
description.service_map.erase (map_it);
}
else
{
throw std::runtime_error("Service with such name was not found map not found!");
}
}

在哪里

struct service_description
{
//A service must have
std::string name;
std::string library_name;
std::string class_name;
std::string root_file_system_directory;
boost::property_tree::ptree service_custome_properties_tree;

//A service might have
std::vector<std::string> set_of_url_rules;
boost::unordered_multimap<std::string, std::string> set_of_header_rules;
boost::unordered_multimap<std::string, std::string> set_of_arguments_rules;
std::set<std::string> url_extensions;
std::string root_service_web_path;
};

但是我得到了奇怪的错误:

error C2784: 'std::pointer_to_binary_function<_Arg1,_Arg2,_Result,_Result(__cdecl *)(_Arg1,_Arg2)> std::ptr_fun(_Result (__cdecl *)(_Arg1,_Arg2))' : could not deduce template argument for '_Result (__cdecl *)(_Arg1,_Arg2)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'

error C2784: 'std::pointer_to_binary_function<_Arg1,_Arg2,_Result,_Result(__fastcall *)(_Arg1,_Arg2)> std::ptr_fun(_Result (__fastcall *)(_Arg1,_Arg2))' : could not deduce template argument for '_Result (__fastcall *)(_Arg1,_Arg2)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'

error C2784: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__cdecl *)(_Arg)> std::ptr_fun(_Result (__cdecl *)(_Arg))' : could not deduce template argument for '_Result (__cdecl *)(_Arg)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'

error C2784: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__stdcall *)(_Arg)> std::ptr_fun(_Result (__stdcall *)(_Arg))' : could not deduce template argument for '_Result (__stdcall *)(_Arg)' from 'bool (__thiscall server_utils::* )(const std::pair<_Ty1,_Ty2> &,std::string) const'

and so on...

我该怎么办?如何修复我的代码? boost.function 或 boost.bind 可以帮助我吗?

最佳答案

使用boost::bind;它更聪明:

map_t::iterator map_it = std::find_if(
description.service_map.begin(), description.service_map.end(),
boost::bind(&server_utils::find_service_by_name_iterator_function, this, name));

关于c++ - 如何遍历这样的 map ?如何修复 8 个可怕的 C2784 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6701917/

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