gpt4 book ai didi

C++ 将 shared_ptr boost 为 hash_map 键

转载 作者:太空狗 更新时间:2023-10-29 23:22:00 25 4
gpt4 key购买 nike

我正在制作一个神经网络,并希望使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com