以下代码无法使用
进行编译错误C3497:您无法构造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);
我注意到有时会在未包含标题时发生这种情况。这些是包含的标题:
#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> ]
我在这里做错了什么?
答案 0 :(得分:0)
在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
};