문제

나는 신경망을 만들고 있으며 각 뉴런에 대한 출력 뉴런에 대한 체중 참조를 유지하기 위해 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의 열쇠로 사용할 수 없다는 것을 알고 있습니다. 그렇다면 또 다른 제안은 무엇입니까? 해결 방법이 있거나 다른 키를 사용하거나 std ::지도로 전환하는 유일한 옵션이 있습니까? 감사!

오류는 다음과 같습니다.

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...
도움이 되었습니까?

해결책

당신은 아마 가지고 있습니다 Neuron .h 파일에 선언되었습니다.

아래는 작동 할 수 있습니다.

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 기본 해싱 기능이 없으므로 (어떤 모습입니까?).

다른 팁

해시에 대한 템플릿 전문화를 제공 할 수도 있습니다.

#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();
    }
};

unordered_set에서 완벽하게 작동합니다.

class Foo;
typedef boost::shared_ptr<Foo> FooPtr;
typedef std::tr1::unordered_set<FooPtr> FooSet;

"정상" Neuron* KEY로 작동해야합니다 hash_map:

typedef stdext::hash_map<Neuron*, WEIGHT> NeuronWeightMap;

조회하고 싶다면 shared_ptr-당신은 사용할 수 있습니다 shared_ptr에스 get() 원시 포인터에 액세스하는 방법 :

NeuronWeightMap weights;
boost::shared_ptr<Neuron> n;

weights.find(n.get());

이미 부스트를 사용하므로 사용하지 않으십시오 boost::unordered_map 해시 테이블을 위해? 상자 밖으로 컴파일합니다 shared_ptr 키의 경우 올바르게 수행하는 방법을 확인하는 것이 좋습니다 (부스트)

위의 코드에서 컴파일러 오류는 무엇입니까?

클래스 뉴런을 선포하는 것을 시도해 보셨습니까?

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();
    //...
};

이 경우 표준 맵도 괜찮아 야합니다. 해시 맵을 사용하는 특정 이유는 무엇입니까?

hash_value () 함수를 추가하여 shared_ptr을 stdext :: hash_map에서 키로 사용할 수 있습니다. 아래는 boost :: unordered_map을 사용하여 std :: hash_map을 비교합니다.

#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>();
}

해시 기능에 대한 문제가 발생합니다 stdext::hash_set 또한. 선언하는 솔루션 hash_value() 메인 헤더 파일에서 한 번만 선언 할 수 있기 때문에 최고입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top