以下代码编译失败
error C3497: you cannot construct an instance of a lambda`:
auto hasher = [&](const cv::Vec3b& color) -> size_t {
std::hash<int> int_hasher;
return int_hasher(color[0]) + int_hasher(color[1]) + int_hasher(color[2]);
// the above code is probably a wrong way of constructing a hash,
// but I'd like to get it to compile first
};
std::unordered_map<cv::Vec3b, int, decltype(hasher)> color_counts(10, hasher);
我注意到有时不包含 header 时会发生这种情况。这些是包含的 header :
#include <unordered_map>
#include <functional>
#include "opencv2/core/core.hpp"
注意:我在 VS 2013 中对优先级队列的比较器使用了相同的技术,并且它有效。我看到有一个使用 std::function 的替代方法,但我想让这个方法起作用。
编辑:完整的错误日志
error C3497: you cannot construct an instance of a lambda
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(164) : while compiling class template member function 'OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824> std::_Hash_oper1<false,_Hasher>::_Gethash(void) const'
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(242) : see reference to function template instantiation 'OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824> std::_Hash_oper1<false,_Hasher>::_Gethash(void) const' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(198) : see reference to class template instantiation 'std::_Hash_oper1<false,_Hasher>' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(220) : see reference to class template instantiation 'std::_Hash_oper2<false,_Hasher,_Keyeq>' being compiled
2> with
2> [
2> _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\unordered_map(23) : see reference to class template instantiation 'std::_Uhash_compare<_Kty,_Hasher,_Keyeq>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\xhash(255) : see reference to class template instantiation 'std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> , _Alloc=std::allocator<std::pair<const cv::Vec3b,int>>
2> ]
2> E:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\unordered_map(81) : see reference to class template instantiation 'std::_Hash<std::_Umap_traits<_Kty,_Ty,std::_Uhash_compare<_Kty,_Hasher,_Keyeq>,_Alloc,false>>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> , _Hasher=OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>
2> , _Keyeq=std::equal_to<cv::Vec3b>
2> , _Alloc=std::allocator<std::pair<const cv::Vec3b,int>>
2> ]
2> oslsegmentation.cpp(42) : see reference to class template instantiation 'std::unordered_map<cv::Vec3b,int,OSLSegmentation::read_images::<lambda_52090ebe4a9b9afa82eb49e6ee9eb824>,std::equal_to<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' being compiled
2> with
2> [
2> _Kty=cv::Vec3b
2> , _Ty=int
2> ]
我在这里做错了什么?
最佳答案
在 Kerrek 的评论后想通了:
改变这个:
auto hasher = [&](const cv::Vec3b& color) -> size_t {
std::hash<int> int_hasher;
return int_hasher(color[0]) + int_hasher(color[1]) + int_hasher(color[2]);
// the above code is probably a wrong way of constructing a hash,
// but I'd like to get it to compile first
};
至(注意引用):
auto& hasher = [&](const cv::Vec3b& color) -> size_t {
std::hash<int> int_hasher;
return int_hasher(color[0]) + int_hasher(color[1]) + int_hasher(color[2]);
// the above code is probably a wrong way of constructing a hash,
// but I'd like to get it to compile first
};
关于c++ - 哈希函数 lambda 无法使用 unordered_map 进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383754/