没有匹配函数来调用 std::transform,未解析的重载函数类型
No matching function for call to std::transform, unresolved overloaded function type
我有以下正确的编译代码:
#include <iostream>
#include <map>
#include <algorithm>
template<typename K, typename V>
void write_map(const std::multimap<K, V> mm)
{
std::cout << "MultiMap content:"<< std::endl << std::endl;
for(auto it = mm.begin(); it != mm.end(); it++)
std::cout << it->first << "\t" << it->second << std::endl;
}
template<typename K, typename V>
void write_map(const std::map<K, V> m)
{
std::cout << "Map content:"<< std::endl << std::endl;
for(auto it = m.begin(); it != m.end(); it++)
std::cout << it->first << "\t" << it->second << std::endl;
}
template<typename KV, typename VK>
std::pair<VK, KV> flip_pair(const std::pair<KV, VK> &p)
{
return std::pair<VK, KV>(p.second, p.first);
}
template<typename KV, typename VK>
std::multimap<VK, KV> flip_map(const std::map<KV, VK> &src)
{
std::multimap<VK, KV> dst;
// LINE_B follows
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), flip_pair<KV, VK>);
return dst;
}
int main(void)
{
std::map<std::string, uint32_t> m_words_map1;
std::pair<std::string, uint32_t> p1("aaa", 2);
std::pair<std::string, uint32_t> p2("bbb", 1);
m_words_map1.insert(p1);
m_words_map1.insert(p2);
write_map(m_words_map1);
// LINE_A follows
std::multimap<uint32_t, std::string> sorted_multimap1 = flip_map(m_words_map1);
write_map(sorted_multimap1);
}
我在 Debian 7.8 x64
、gcc 4.7.2
和
上成功编译
g++ foo.cpp -o foo -std=c++11
之后,我将这一堆功能嵌入到一个使用CMake的复杂项目中(这里是CMakeLists.txt)
file(GLOB_RECURSE CORE_OBJ core/*.cpp)
file(GLOB_RECURSE LIB_OBJ_CPP lib/*.cpp)
file(GLOB_RECURSE LIB_OBJ_HPP lib/*.hpp)
file(GLOB_RECURSE LIB_OBJ_C lib/*.c)
list(APPEND LIB_OBJ ${LIB_OBJ_CPP} ${LIB_OBJ_HPP} ${LIB_OBJ_C})
file(GLOB_RECURSE USR_CPP usr/*.cpp)
file(GLOB_RECURSE USR_HPP usr/*.hpp)
list(APPEND USR_OBJ ${USR_CPP} ${USR_HPP})
list(APPEND BLOCKMON_LINK_LIBS pcap pthread boost_system boost_filesystem)
list(APPEND INCLUDE_DIRS lib/external/pugixml/src lib lib/external/factory lib/external/more lib/external/ lib/hash lib/bloom lib/pat core messages blocks ipfix core/block core/message core/composition core/scheduler core/time ${USR_INCL})
include_directories( ${INCLUDE_DIRS} )
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -march=native -Wall -g -std=c++0x")
add_definitions(-D_GLIBCXX_USE_SCHED_YIELD -D_GLIBCXX_USE_NANOSLEEP)
target_link_libraries(blockmonpy blockmoncore boost_python)
link_directories(/usr/local/lib)
list(APPEND BLOCKMON_XML-RPC_LINK_LIBS xmlrpc_client++ xmlrpc_client xmlrpc++ xmlrpc xmlrpc_util xmlrpc_xmlparse xmlrpc_xmltok curl xmlrpc_packetsocket xmlrpc_server_abyss++ xmlrpc_server++ xmlrpc_server_abyss xmlrpc_server xmlrpc_abyss pthread)
list(APPEND BLOCKMON_LINK_LIBS ${BLOCKMON_XML-RPC_LINK_LIBS})
target_link_libraries(blockmonWithXmlRPC blockmoncore ${BLOCKMON_LINK_LIBS})
add_library(blockmoncore SHARED ${CORE_OBJ} ${MSG_OBJ} ${IPFIX_OBJ} ${LIB_OBJ} ${BLOCKS_OBJ} ${USR_OBJ})
target_link_libraries(blockmoncore ${BLOCKMON_LINK_LIBS})
add_executable(blockmon bin/blockmon.cpp)
target_link_libraries(blockmon blockmoncore ${BLOCKMON_LINK_LIBS})
在这个项目的源文件中,代码段被镜像到一个源文件中,没有任何修改。但是在这里,make
停止并抱怨以下内容。我调整了输出以更好地绑定带有错误的行号:
In instantiation of ‘std::multimap<VK, KV> flip_map(const std::map<K, V>&) [with KV = std::basic_string<char>; VK = unsigned int]’:
LINE A: required from here
LINE B: error: no matching function for call to ‘transform(std::map<std::basic_string<char>, unsigned int>::const_iterator, std::map<std::basic_string<char>, unsigned int>::const_iterator, std::insert_iterator<std::multimap<unsigned int, std::basic_string<char> > >, <unresolved overloaded function type>)’
note: candidates are:
note: _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, unsigned int> >; _OIter = std::insert_iterator<std::multimap<unsigned int, std::basic_string<char> > >; _UnaryOperation = std::pair<unsigned int, std::basic_string<char> > (Class::*)(const std::pair<std::basic_string<char>, unsigned int>&)]
note: no known conversion for argument 4 from ‘<unresolved overloaded function type>’ to ‘std::pair<unsigned int, std::basic_string<char> > (Class::*)(const std::pair<std::basic_string<char>, unsigned int>&)’
/usr/include/c++/4.7/bits/stl_algo.h:4977:5: note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
/usr/include/c++/4.7/bits/stl_algo.h:4977:5: note: template argument deduction/substitution failed:
LINE B: note: candidate expects 5 arguments, 4 provided
查看您的错误消息 (grep <unresolved overloaded function type>
),我的猜测是您以某种方式重载了传递给 [= 的函数 flip_pair<>
12=] 在其他翻译单元中。 std::transform
is a template function, and its functor is passed as a template type. If you have overloads, then std::transform
won't be able to deduce the type of the transforming function, hence the error. See a toy example here,它吐出基本上相同的错误消息。
我有以下正确的编译代码:
#include <iostream>
#include <map>
#include <algorithm>
template<typename K, typename V>
void write_map(const std::multimap<K, V> mm)
{
std::cout << "MultiMap content:"<< std::endl << std::endl;
for(auto it = mm.begin(); it != mm.end(); it++)
std::cout << it->first << "\t" << it->second << std::endl;
}
template<typename K, typename V>
void write_map(const std::map<K, V> m)
{
std::cout << "Map content:"<< std::endl << std::endl;
for(auto it = m.begin(); it != m.end(); it++)
std::cout << it->first << "\t" << it->second << std::endl;
}
template<typename KV, typename VK>
std::pair<VK, KV> flip_pair(const std::pair<KV, VK> &p)
{
return std::pair<VK, KV>(p.second, p.first);
}
template<typename KV, typename VK>
std::multimap<VK, KV> flip_map(const std::map<KV, VK> &src)
{
std::multimap<VK, KV> dst;
// LINE_B follows
std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()), flip_pair<KV, VK>);
return dst;
}
int main(void)
{
std::map<std::string, uint32_t> m_words_map1;
std::pair<std::string, uint32_t> p1("aaa", 2);
std::pair<std::string, uint32_t> p2("bbb", 1);
m_words_map1.insert(p1);
m_words_map1.insert(p2);
write_map(m_words_map1);
// LINE_A follows
std::multimap<uint32_t, std::string> sorted_multimap1 = flip_map(m_words_map1);
write_map(sorted_multimap1);
}
我在 Debian 7.8 x64
、gcc 4.7.2
和
g++ foo.cpp -o foo -std=c++11
之后,我将这一堆功能嵌入到一个使用CMake的复杂项目中(这里是CMakeLists.txt)
file(GLOB_RECURSE CORE_OBJ core/*.cpp)
file(GLOB_RECURSE LIB_OBJ_CPP lib/*.cpp)
file(GLOB_RECURSE LIB_OBJ_HPP lib/*.hpp)
file(GLOB_RECURSE LIB_OBJ_C lib/*.c)
list(APPEND LIB_OBJ ${LIB_OBJ_CPP} ${LIB_OBJ_HPP} ${LIB_OBJ_C})
file(GLOB_RECURSE USR_CPP usr/*.cpp)
file(GLOB_RECURSE USR_HPP usr/*.hpp)
list(APPEND USR_OBJ ${USR_CPP} ${USR_HPP})
list(APPEND BLOCKMON_LINK_LIBS pcap pthread boost_system boost_filesystem)
list(APPEND INCLUDE_DIRS lib/external/pugixml/src lib lib/external/factory lib/external/more lib/external/ lib/hash lib/bloom lib/pat core messages blocks ipfix core/block core/message core/composition core/scheduler core/time ${USR_INCL})
include_directories( ${INCLUDE_DIRS} )
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -march=native -Wall -g -std=c++0x")
add_definitions(-D_GLIBCXX_USE_SCHED_YIELD -D_GLIBCXX_USE_NANOSLEEP)
target_link_libraries(blockmonpy blockmoncore boost_python)
link_directories(/usr/local/lib)
list(APPEND BLOCKMON_XML-RPC_LINK_LIBS xmlrpc_client++ xmlrpc_client xmlrpc++ xmlrpc xmlrpc_util xmlrpc_xmlparse xmlrpc_xmltok curl xmlrpc_packetsocket xmlrpc_server_abyss++ xmlrpc_server++ xmlrpc_server_abyss xmlrpc_server xmlrpc_abyss pthread)
list(APPEND BLOCKMON_LINK_LIBS ${BLOCKMON_XML-RPC_LINK_LIBS})
target_link_libraries(blockmonWithXmlRPC blockmoncore ${BLOCKMON_LINK_LIBS})
add_library(blockmoncore SHARED ${CORE_OBJ} ${MSG_OBJ} ${IPFIX_OBJ} ${LIB_OBJ} ${BLOCKS_OBJ} ${USR_OBJ})
target_link_libraries(blockmoncore ${BLOCKMON_LINK_LIBS})
add_executable(blockmon bin/blockmon.cpp)
target_link_libraries(blockmon blockmoncore ${BLOCKMON_LINK_LIBS})
在这个项目的源文件中,代码段被镜像到一个源文件中,没有任何修改。但是在这里,make
停止并抱怨以下内容。我调整了输出以更好地绑定带有错误的行号:
In instantiation of ‘std::multimap<VK, KV> flip_map(const std::map<K, V>&) [with KV = std::basic_string<char>; VK = unsigned int]’:
LINE A: required from here
LINE B: error: no matching function for call to ‘transform(std::map<std::basic_string<char>, unsigned int>::const_iterator, std::map<std::basic_string<char>, unsigned int>::const_iterator, std::insert_iterator<std::multimap<unsigned int, std::basic_string<char> > >, <unresolved overloaded function type>)’
note: candidates are:
note: _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, unsigned int> >; _OIter = std::insert_iterator<std::multimap<unsigned int, std::basic_string<char> > >; _UnaryOperation = std::pair<unsigned int, std::basic_string<char> > (Class::*)(const std::pair<std::basic_string<char>, unsigned int>&)]
note: no known conversion for argument 4 from ‘<unresolved overloaded function type>’ to ‘std::pair<unsigned int, std::basic_string<char> > (Class::*)(const std::pair<std::basic_string<char>, unsigned int>&)’
/usr/include/c++/4.7/bits/stl_algo.h:4977:5: note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)
/usr/include/c++/4.7/bits/stl_algo.h:4977:5: note: template argument deduction/substitution failed:
LINE B: note: candidate expects 5 arguments, 4 provided
查看您的错误消息 (grep <unresolved overloaded function type>
),我的猜测是您以某种方式重载了传递给 [= 的函数 flip_pair<>
12=] 在其他翻译单元中。 std::transform
is a template function, and its functor is passed as a template type. If you have overloads, then std::transform
won't be able to deduce the type of the transforming function, hence the error. See a toy example here,它吐出基本上相同的错误消息。