problem mit std::set



  • Hallo,

    ich habe ein problem mit der Benutzung des containers std::set. Wenn ich diesen benutzen will gibt mir der gcc eine lange liste fehler aus.

    /usr/include/c++/4.3/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree_node<Service>’:
    /usr/include/c++/4.3/bits/stl_tree.h:941:   instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = Service, _Val = Service, _KeyOfValue = std::_Identity<Service>, _Compare = std::less<Service>, _Alloc = std::allocator<Service>]’
    /usr/include/c++/4.3/bits/stl_tree.h:585:   instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = Service, _Val = Service, _KeyOfValue = std::_Identity<Service>, _Compare = std::less<Service>, _Alloc = std::allocator<Service>]’
    /usr/include/c++/4.3/bits/stl_set.h:93:   instantiated from ‘void __gnu_cxx::new_allocator<_Tp>::destroy(_Tp*) [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > >]’
    /usr/include/c++/4.3/bits/stl_tree.h:390:   instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >]’
    /usr/include/c++/4.3/bits/stl_tree.h:943:   instantiated from ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >]’
    /usr/include/c++/4.3/bits/stl_tree.h:585:   instantiated from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<Service, std::less<Service>, std::allocator<Service> > > >]’
    /usr/include/c++/4.3/bits/stl_map.h:92:   instantiated from here
    /usr/include/c++/4.3/bits/stl_tree.h:135: error: cannot declare field ‘std::_Rb_tree_node<Service>::_M_value_field’ to be of abstract type ‘Service’
    Service.h:12: note:   because the following virtual functions are pure within ‘Service’:
    Service.h:17: note: 	virtual void Service::service()
    

    Nun kann ich leider nix mit diesen Meldungen anfangen. Ich poste euch noch die 2 beteiligten klassen service und dispatcher. Mit dem container std::vector habe ich die Probleme nicht. Würde aber gerne std::set benutzen da die "Services" nur einmal in dem container vorkommen sollten. Hat mir vlt jemand einen rat oder tip? Die Fehler treten nur beim compilen von der Klasse Dispatcher auf.

    Hier noch die 2 Klassen Definitionen:

    Dispatcher

    #ifndef _DISPATCHER_H
    #define _DISPATCHER_H
    
    #include <map>
    #include <set>
    #include <vector>
    #include <string>
    #include "Service.h"
    
    class Service;
    
    class Dispatcher
    {
    	private:
    		//Methods
    		Dispatcher();
    		~Dispatcher();
    
    		//Members
    		static Dispatcher *uniqueInstance;
    
    		//stores service and its mirrors
    		typedef std::map<std::string, std::set<Service> > myMapType;
    		 myMapType serviceList;
    
           int randomIndex(const int&);
    	public:
          static Dispatcher* getInstance();
    		static Dispatcher* instance();
    
    		Service& locate(const std::string&);
    
          void registerService(Service&);
          void deregisterService(Service&);
    };
    
    #endif
    

    und noch die Klasse Service

    #ifndef _SERVICE_H
    #define _SERVICE_H
    
    #include <string>
    #include <map>
    #include <vector>
    #include "Dispatcher.h"
    
    class Dispatcher;
    
    class Service
    {
    	public:
    		Service(const std::string& type, const std::string& prov);
          Service(const Service&);
    		virtual ~Service();
    		virtual void service()=0;
    
          // Getter
          virtual std::string getProvider() const;
          virtual std::string getServiceType() const;
    
          // Setter
          virtual void setServiceType(const std::string&);
          virtual void setProvider(const std::string&);
    
          // operators
          virtual Service& operator=(const Service&);
          friend bool operator<(const Service& val1, const Service& val2)
          {
             return (val1.provider < val2.provider);
          }
    
       protected:
          // Methods
    		virtual void registerAtDispatcher();
    		virtual void deregisterFromDispatcher();
    
          // Members
          Dispatcher *disp;
          std::string serviceType;
          std::string provider;
    
    };
    
    #endif
    

    Falls ihr auch noch die cpp datein braucht sagt bescheid.

    MFG
    blups0r



  • Lösch doch einfach die Zeile #include "Dispatcher.h" aus Service.h , dann sollte es gehen.

    Wenn du lang und scharf nachdenkst, wirst du vermutlich auch draufkommen wieso. Übrigens kann es auch mit std::vector nicht gehen. Vermutlich hast du zwei Änderungen gleichzeitig gemacht, und die falsche verdächtigt.



  • ich hab das ausprobiert aber wenn ich #inlcude "Dispatcher.h" lösche läasst sich service nicht mehr compilieren. Dann kennt g++ die klasse Dispatcher nicht und meckert deswegen. Hast vlt noch nen anderen vorschlag?



  • In der Service-Implementierungsdatei (Service.cpp?) noch " #include "Dispatcher.h " hinzufügen.



  • Service ist abstrakt - das kann man nicht by value in einen container packen... Muss man die boost pointer container nehmen...



  • mkay. hab das Problem anders gelöst trotzdem danke für eure hilfe



  • Huch!
    Das ist ja wirklich ein =0 im Service 🙂
    Hatte ich übersehen.

    In dem Fall löst man das über boost::shared_ptr<Service> oder ähnlichem.
    Natürlich sollte man erstmal verstehen WARUM das wie im Kopfposting zu sehen nicht gehen kann...
    Container != Collection und so, ne.


Anmelden zum Antworten