C++ 将 shared_ptr boost 为 hash_map 键

标签 c++ boost neural-network shared-ptr hashmap

我正在制作一个神经网络,并希望使用 hash_map 为每个神经元保留输出神经元的权重引用:

class Neuron; //forward declaration was there (sorry I forgot to show it earlier)
typedef double WEIGHT;
typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT> NeuronWeightMap;
class Neuron
{
private:
    NeuronWeightMap m_outputs;
    //...
public:

    Neuron();
    ~Neuron();
    //...
    WEIGHT GetWeight(const boost::shared_ptr<Neuron>& neuron) const
    {
        NeuronWeightMap::const_iterator itr = m_outputs.find(neuron);
        if( itr != m_outputs.end() )
        {
            return itr->second;
        }
        return 0.0f;
    }
};

我意识到我不能将 boost::shared_ptr 用作 stdext::hash_map 的键,那么另一个建议是什么?是否有任何解决方法,或者是使用不同 key 或可能切换到 std::map 的唯一选择? 谢谢!

这里是错误:

1>c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(61) : error C2440: 'type cast' : cannot convert from 'const boost::shared_ptr<T>' to 'size_t'
1>        with
1>        [
1>            T=Neuron
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(99) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(98) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &) const'
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(80) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(119) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT,
1>            _Tr=stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,
1>            _Alloc=std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,
1>            _Mfl=false
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(90) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=stdext::_Hmap_traits<boost::shared_ptr<Neuron>,common_ns::WEIGHT,stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,false>
1>        ]
1>        FILE_PATH_REMOVED\neuralnet.h(21) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT
1>        ]
1>NeuralNet.cpp
1>c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(61) : error C2440: 'type cast' : cannot convert from 'const boost::shared_ptr<T>' to 'size_t'
1>        with
1>        [
1>            T=Neuron
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(99) : see reference to function template instantiation 'size_t stdext::hash_value<_Kty>(const _Kty &)' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(98) : while compiling class template member function 'size_t stdext::hash_compare<_Kty,_Pr>::operator ()(const _Kty &) const'
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(80) : see reference to class template instantiation 'stdext::hash_compare<_Kty,_Pr>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Pr=std::less<boost::shared_ptr<Neuron>>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xhash(119) : see reference to class template instantiation 'stdext::_Hmap_traits<_Kty,_Ty,_Tr,_Alloc,_Mfl>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT,
1>            _Tr=stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,
1>            _Alloc=std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,
1>            _Mfl=false
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\hash_map(90) : see reference to class template instantiation 'stdext::_Hash<_Traits>' being compiled
1>        with
1>        [
1>            _Traits=stdext::_Hmap_traits<boost::shared_ptr<Neuron>,common_ns::WEIGHT,stdext::hash_compare<boost::shared_ptr<Neuron>,std::less<boost::shared_ptr<Neuron>>>,std::allocator<std::pair<const boost::shared_ptr<Neuron>,common_ns::WEIGHT>>,false>
1>        ]
1>        FILE_PATH_REMOVED\neuralnet.h(21) : see reference to class template instantiation 'stdext::hash_map<_Kty,_Ty>' being compiled
1>        with
1>        [
1>            _Kty=boost::shared_ptr<Neuron>,
1>            _Ty=common_ns::WEIGHT
1>        ]
1>Generating Code...

最佳答案

您可能在 .h 文件中声明了 Neuron

所以下面可以工作:

struct hasher {
    size_t operator()(const boost::shared_ptr<Neuron>& n) { return (size_t)n.get(); }
};

typedef stdext::hash_map<boost::shared_ptr<Neuron>,WEIGHT,hasher> NeuronWeightMap;

问题可能是 boost:shared_ptr 没有默认的散列函数(它看起来像什么?),所以您只需要提供一个。

关于C++ 将 shared_ptr boost 为 hash_map 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/900369/

相关文章:

c++ -::boost::spirit: +alnum_p 和连字符

python - 无法挤压 dim[1],预期维度为 1,结果为 5

python - Pybrain前馈神经网络训练错误完全卡住

c++ - 为了清楚起见,您是否应该将 .h 文件中已经包含的所有内容都包含在 .cpp 文件中?

c++ - 我可以将 Visual C++ Redistributable 2010 和/或 DirectX 10 安装到特定文件夹吗?

c++ - 使用 CvMoments 计算 HU 矩时出错

python - pytorch 模型中的参数如何不是叶子并且在计算图中?

c++ - 如何更改集合中的对象子成员

c++ - 'boost::make_shared':对重载函数的模糊调用

c++ - 如果在 Windows 上使用 boost managed_shared_memory 会崩溃