散列函数lambda无法使用unordered_map进行编译 [英] hashing function lambda failing to compile with unordered_map

查看:766
本文介绍了散列函数lambda无法使用unordered_map进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法使用

编译:


错误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]);
//上面的代码可能是一个错误的构造一个散列的方法,
//但我想让它先编译
};

std :: unordered_map< cv :: Vec3b,int,decltype(hasher)> color_counts(10,hasher);

我注意到,有时这种情况发生时,不包括标题。这些是包含的标题:

  #include< unordered_map> 
#include< functional>
#includeopencv2 / core / core.hpp

注意:技术用于VS 2013中的优先级队列的比较器,并且它工作。我看到有一个选择使用std :: function,但我想让这个方法工作。



编辑:完成错误日志

 错误C3497:您不能构造lambda的实例
2> E:\Program Files(x86)\Microsoft Visual Studio 12.0 \VC\include\xhash(164):在编译类模板成员函数'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):请参阅对函数模板实例化的引用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(198):参见类模板实例化'std :: _ Hash_oper1< false,_Hasher& b 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):参见类模板实例化'std :: _ Hash_oper2< false,_Hasher,_Keyeq& b $ b 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):参见类模板实例化的参考'std :: _ Uhash_compare< _Kty,_Hasher,_Keyeq& b $ b 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):参见类模板实例化的引用'std :: _ Umap_traits <_Kty,_Ty,std :: _Uhash_compare< _Kty ,_Hasher,_Keyeq>,_ Alloc,false>'编译
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):参见类模板实例化的参考std :: _ Hash< std :: _ Umap_traits< _Kty,_Ty,std :: _ Uhash_compare< _Kty,_Hasher,_Keyeq>,_ Alloc,false>>'正在编译
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):参见类模板实例化的引用'std :: unordered_map< cv :: Vec3b,int,OSLSegmentation :: read_images ::< lambda_52090ebe4a9b9afa82eb49e6ee9eb824>,std :: equal_to< _Kty>,std :: allocator& std :: pair< const _Kty,_Ty>>>'正在编译
2> with
2> [
2> _Kty = cv :: Vec3b
2> ,_Ty = int
2> ]

我在这里做错了什么?



改变这个:

pre> 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]);
//上面的代码可能是一个错误的构造一个散列的方法,
//但我想让它先编译
};

(注意参考):

  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 following code is failing to compile with

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);

I noticed sometimes this happens when headers are not included. These are the included headers:

#include <unordered_map>
#include <functional>
#include "opencv2/core/core.hpp"

Note: I use the same technique for a comparator for priority queues in VS 2013, and it works. I see there's an alternative using std::function but I'd like to make this method work.

EDIT: Complete error log

 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>          ]

What am I doing wrong here?

解决方案

Figured it out after Kerrek's comment:

change this:

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
    };

to (note the reference):

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
    };

这篇关于散列函数lambda无法使用unordered_map进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆