C++ C++;将共享\u ptr提升为哈希映射键

C++ C++;将共享\u ptr提升为哈希映射键,c++,boost,neural-network,shared-ptr,hashmap,C++,Boost,Neural Network,Shared Ptr,Hashmap,我正在制作一个神经网络,想用一个hash_映射为每个神经元的输出神经元保留权重参考: 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 {

我正在制作一个神经网络,想用一个hash_映射为每个神经元的输出神经元保留权重参考:

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;
    }
};
类神经元//转发声明在那里(对不起,我忘了早些时候出示)
typedef双倍重量;
typedef stdext::hash_映射;
类神经元
{
私人:
地图m_输出;
//...
公众:
神经元();
~Neuron();
//...
权重GetWeight(const boost::shared_ptr&neuron)const
{
NeuronWeightMap::const_迭代器itr=m_outputs.find(neuron);
if(itr!=m_outputs.end())
{
返回itr->second;
}
返回0.0f;
}
};
我意识到我不能使用boost::shared_ptr作为stdext::hash_映射的键,那么还有什么建议呢?是否有任何解决方法,或者是使用其他键或切换到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...
1>c:\program files(x86)\microsoft visual studio 8\vc\include\xhash(61):错误C2440:“类型转换”:无法从“const boost::shared\u ptr”转换为“size\t”
1> 与
1>        [
1> T=神经元
1>        ]
1> 没有可执行此转换的用户定义的转换运算符,或者无法调用该运算符
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(99):请参阅正在编译的函数模板实例化“size\t stdext::hash\u value(const\u Kty&)”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(98):编译类模板成员函数“size\t stdext::hash\u compare::operator()(const\u Kty&)const”时
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Pr=std::less
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\hash\u map(80):请参阅正在编译的类模板实例化“stdext::hash\u compare”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Pr=std::less
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(119):请参阅正在编译的类模板实例化“stdext::\u Hmap\u traits”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Ty=普通重量:,
1> _Tr=stdext::hash\u compare,
1> _Alloc=std::分配器,
1> _Mfl=错误
1>        ]
1> c:\program files(x86)\microsoft visual studio 8\vc\include\hash\u map(90):请参阅正在编译的类模板实例化“stdext::\u hash”的参考
1> 与
1>        [
1> _Traits=stdext::_Hmap_Traits
1>        ]
1> 文件\u PATH\u REMOVED\neuralnet.h(21):请参阅对正在编译的类模板实例化'stdext::hash\u map'的引用
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Ty=普通重量::重量
1>        ]
1> NeuralNet.cpp
1> c:\program files(x86)\microsoft visual studio 8\vc\include\xhash(61):错误C2440:“类型转换”:无法从“const boost::shared\u ptr”转换为“size\t”
1> 与
1>        [
1> T=神经元
1>        ]
1> 没有可执行此转换的用户定义的转换运算符,或者无法调用该运算符
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(99):请参阅正在编译的函数模板实例化“size\t stdext::hash\u value(const\u Kty&)”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(98):编译类模板成员函数“size\t stdext::hash\u compare::operator()(const\u Kty&)const”时
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Pr=std::less
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\hash\u map(80):请参阅正在编译的类模板实例化“stdext::hash\u compare”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Pr=std::less
1>        ]
1> c:\ProgramFiles(x86)\microsoft visual studio 8\vc\include\xhash(119):请参阅正在编译的类模板实例化“stdext::\u Hmap\u traits”的参考
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Ty=普通重量:,
1> _Tr=stdext::hash\u compare,
1> _Alloc=std::分配器,
1> _Mfl=错误
1>        ]
1> c:\program files(x86)\microsoft visual studio 8\vc\include\hash\u map(90):请参阅正在编译的类模板实例化“stdext::\u hash”的参考
1> 与
1>        [
1> _Traits=stdext::_Hmap_Traits
1>        ]
1> 文件\u PATH\u REMOVED\neuralnet.h(21):请参阅对正在编译的类模板实例化'stdext::hash\u map'的引用
1> 与
1>        [
1> _Kty=boost::shared_ptr,
1> _Ty=普通重量::重量
1>        ]
1> 正在生成代码。。。

从上面的代码可以看出编译器错误是什么

您是否尝试过向前声明类神经元

class Neuron; // class Neuron must be forward declared to be used

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


class Neuron
{
private:
    NeuronWeightMap m_outputs;
    //...
public:

    Neuron();
    ~Neuron();
    //...
};
类神经元;//类Neuron必须向前声明才能使用
typedef双倍重量;
typedef stdext::hash_映射;
类神经元
{
私人:
地图m_输出;
//...
公众:
神经元();
~Neuron();
//...
};

在这种情况下,标准映射也可以,您使用哈希映射的具体原因是什么?

您可能在.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;
结构哈希器{ size_t操作符()(const boost::shared_ptr&n){return(size_t)n.get()} }; typedef stdext::hash_映射; 问题可能在于
boost:shared\u ptr
没有默认的散列函数(散列函数是什么样子的?),所以您只需要提供一个
typedef stdext::hash_map<Neuron*, WEIGHT> NeuronWeightMap;
NeuronWeightMap weights;
boost::shared_ptr<Neuron> n;

weights.find(n.get());
#include <boost/smart_ptr.hpp>
#include <boost/unordered_map.hpp>
#include <hash_map>
#include <assert.h>

struct MyItem
{
    int i;
};
typedef boost::shared_ptr<const MyItem> MyItem_csptr;

struct MyExtra
{
    double d;
};

//Boost's unordered_map already knows how to hash shared_ptr
typedef boost::unordered_map<MyItem_csptr,MyExtra> MapMyItemToExtra;


//But for stdext::hash_map, we need to write this:
template<class T>
size_t hash_value(const boost::shared_ptr<T>& aSptr)
{
    return reinterpret_cast<size_t>( aSptr.get() );
};
typedef stdext::hash_map<MyItem_csptr,MyExtra> MapMyItemToExtra2;


template<class MAP>
void TryMapMyItemToExtra_T()
{
    MAP tMap;

    MyItem_csptr tItem1(new MyItem);
    MyItem_csptr tItem2(new MyItem);
    tMap[tItem1].d=1.0;
    tMap[tItem1].d=1.1;
    tMap[tItem2].d=2.0;

    assert( tMap[tItem1].d == 1.1 );
    assert( tMap[tItem2].d == 2.0 );
}

void TryMapMyItemToExtra()
{
    TryMapMyItemToExtra_T<MapMyItemToExtra>();
    TryMapMyItemToExtra_T<MapMyItemToExtra2>();
}
#include <functional>
#include <boost/shared_ptr.hpp>

template<class T>
class std::tr1::hash<boost::shared_ptr<T>> {
public:
    size_t operator()(const boost::shared_ptr<T>& key) const {
        return (size_t)key.get();
    }
};
class Foo;
typedef boost::shared_ptr<Foo> FooPtr;
typedef std::tr1::unordered_set<FooPtr> FooSet;