set von auto_ptr und find_if einer id



  • Aaaaalso, ich habe eine Klasse Identifier. Diese sieht so aus:

    /*
     * an identifyable object
     */
    class Identifier {
    private:
    	static ident_t maxID;
    	ident_t ID;
    public:
    	typedef auto_ptr<Identifier> ptr;
    	Identifier();
    	void createIdentifier();
    	ident_t getIdentifier();
    
    	bool operator<(Identifier::ptr &b);
    	bool operator<(ident_t i);
    	bool operator==(ident_t i);
    
    };
    

    dem folgt eine binary_function zum Vergleichen:

    class CompareIdentifier: binary_function<Identifier::ptr , ident_t, bool>
    {
    public:
       result_type operator( ) ( first_argument_type a, second_argument_type id)
       {
    		return a->getIdentifier() == id;
       }
    };
    

    jetzt kommt ein set<Identifier::ptr, Identifier>, nennen wir es memory.
    Wenn ich anhand der ID etwas im set finden will, denkt ich mir, nehm ich die find_if-funktion:

    Identifier *find(ident_t id) {
    		iter it;
    		iter right;
    		it = find_if(memory.begin(), memory.end(),
    				bind2nd( CompareIdentifier, id)
    				);
    		return (*it).get();
    	}
    

    beim Kompilieren bekomme ich folgende Ausgabe

    **** Build of configuration Debug for project 01SemaNet ****

    make all
    Building file: ../src/MemoryNodes.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/MemoryNodes.d" -MT"src/MemoryNodes.d" -o"src/MemoryNodes.o" "../src/MemoryNodes.cpp"
    In file included from ../src/MemoryNodes.cpp:8:
    ../src/MemoryNodes.h: In member function ‘Identifier* Memory::find(ident_t)’:
    ../src/MemoryNodes.h:79: error: expected primary-expression before ‘,’ token
    /usr/include/c++/4.3/bits/stl_tree.h: In member function ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’:
    /usr/include/c++/4.3/bits/stl_set.h:381: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::auto_ptr<Identifier>, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    ../src/MemoryNodes.h:72: instantiated from here
    /usr/include/c++/4.3/bits/stl_tree.h:1141: error: no match for call to ‘(Identifier) (const std::auto_ptr<Identifier>&, const std::auto_ptr<Identifier>&)’
    /usr/include/c++/4.3/bits/stl_set.h:381: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::auto_ptr<Identifier>, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    ../src/MemoryNodes.h:72: instantiated from here
    /usr/include/c++/4.3/bits/stl_tree.h:1152: error: no match for call to ‘(Identifier) (const std::auto_ptr<Identifier>&, const std::auto_ptr<Identifier>&)’
    /usr/include/c++/4.3/bits/stl_tree.h: In member function ‘typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’:
    /usr/include/c++/4.3/bits/stl_tree.h:1148: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    /usr/include/c++/4.3/bits/stl_set.h:381: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::auto_ptr<Identifier>, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    ../src/MemoryNodes.h:72: instantiated from here
    /usr/include/c++/4.3/bits/stl_tree.h:850: error: no match for call to ‘(Identifier) (const std::auto_ptr<Identifier>&, const std::auto_ptr<Identifier>&)’
    /usr/include/c++/4.3/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = std::auto_ptr<Identifier>]’:
    /usr/include/c++/4.3/bits/stl_tree.h:368: instantiated from ‘std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    /usr/include/c++/4.3/bits/stl_tree.h:852: instantiated from ‘typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_(const std::_Rb_tree_node_base*, const std::_Rb_tree_node_base*, const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    /usr/include/c++/4.3/bits/stl_tree.h:1148: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = std::auto_ptr<Identifier>, _Val = std::auto_ptr<Identifier>, _KeyOfValue = std::_Identity<std::auto_ptr<Identifier> >, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    /usr/include/c++/4.3/bits/stl_set.h:381: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = std::auto_ptr<Identifier>, _Compare = Identifier, _Alloc = std::allocator<std::auto_ptr<Identifier> >]’
    ../src/MemoryNodes.h:72: instantiated from here
    /usr/include/c++/4.3/ext/new_allocator.h:108: error: passing ‘const std::auto_ptr<Identifier>’ as ‘this’ argument of ‘std::auto_ptr<_Tp>::operator std::auto_ptr_ref<_Tp1>() [with _Tp1 = Identifier, _Tp = Identifier]’ discards qualifiers
    make: *** [src/MemoryNodes.o] Fehler 1

    so ganz versteh ich die ausgabe allerdings nicht..
    kann mir wer helfen?


  • Mod

    DocJunioR schrieb:

    dem folgt eine binary_function zum Vergleichen:

    class CompareIdentifier: binary_function<Identifier::ptr , ident_t, bool>
    {
    public:
       result_type operator( ) ( first_argument_type a, second_argument_type id)
       {
    		return a->getIdentifier() == id;
       }
    };
    

    Das ist schon mal kritisch, weil hier der auto_ptr per value übergeben wird.

    jetzt kommt ein set<Identifier::ptr, Identifier>

    auto_ptr ist nicht containerkompatibel.



  • das ja man doof
    also eher nen weak pointer`?



  • DocJunioR schrieb:

    das ja man doof
    also eher nen weak pointer`?

    Mehrere Möglichkeiten:
    a) shared_ptr (oder vergleichbares; boost oder tr1).
    b) Pointercontainer wie die von boost.
    c) Selbst um die Verwaltung kümmern.


Anmelden zum Antworten