2012-02-17 29 views
0

所以,我有這樣的代碼:的std ::設置怪異<超載錯誤

#include <string> 
#include <set> 

class Section; 
class Config; 

class SettingBase { 
public: 
    virtual ~SettingBase() { } 
}; 

template<typename T> 
class Setting : private SettingBase { 
    friend class Section; 
public: 
    std::string const &get_name() { return name; } 
    T const &get_value() { return value; } 

private: 
    Setting(std::string name, T value) : name(name), value(value) { } 

    std::string name; 
    T value; 
}; // Class Setting 

class Section { 
    friend class Config; 
public: 
    std::string const &get_name() { return name; } 

    template<typename T> 
    void add(std::string const &name, T const &value) { 
     settings.insert(new Setting<T>(name, value)); 

     return; 
    } 

    template<typename T> 
    bool remove(std::string const &name) { 
     std::set<SettingBase*>::iterator it; 

     for (it = settings.begin(); it != settings.end(); it++) { 
      if ((static_cast< Setting<T> *>(*it))->name.compare(name) == 0) { 
       settings.erase(*it); 
       return true; 
      } 
     } 

     return false; 
    } 

    template<typename T> 
    T const &get(std::string const &name) { 
     std::set<SettingBase*>::iterator it; 

     for (it = settings.begin(); it != settings.end(); it++) { 
      if ((static_cast< Setting<T> *>(*it))->name.compare(name) == 0) return (static_cast< Setting<T> * >(*it))->value; // fuuuuuck. 
     } 

     // TODO: exception 
     throw; 
    } 

private: 
    Section(std::string name) : name(name) { } 

    std::string name; 
    std::set<SettingBase*> settings; 
}; // Class Section 

class Config { 
public: 
    Config(std::string filename) : filename(filename) { }; 

    void add(std::string const &name) { 
     sections.insert(Section(name)); // here's the error. 

     return; 
    } 

    bool remove(std::string const &name) { 
     std::set<Section>::iterator it; 

     // TODO: exceptions 
     for (it = sections.begin(); it != sections.end(); it++) { 
      if ((*it).name.compare(name) == 0) { 
       sections.erase(*it); 
       return true; 
      } 
     } 

     return false; 
    } 


    Section const &get(std::string const &name) { 
     std::set<Section>::iterator it; 

     for (it = sections.begin(); it != sections.end(); it++) { 
      if ((*it).name.compare(name) == 0) return *it; 
     } 

     // TODO: exception 
     throw; 
    } 

private: 
    std::string filename; 
    std::set<Section> sections; 
}; // Class Config 


int main(int argc, char *argv[]) { 
    Config tmp(std::string("this should be a file.txt")); 
    tmp.add(std::string("this is a section's name")); 
    Section sec = tmp.get(std::string("this is a section's name")); 
    sec.add<int>(std::string("test"), 46541); 

    return sec.get<int>(std::string("test")); 

} 

不能編譯,因爲:

[email protected] ~/Workspace/PlusConfig/src $ gcc plusconfig.cpp 
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.3/include/g++-v4/string:50:0, 
       from plusconfig.cpp:4: 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = Section]’: 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_tree.h:1184:4: instantiated from ‘std::pair<std::_Rb_tree_iterator<_Val>, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = Section, _Val = Section, _KeyOfValue = std::_Identity<Section>, _Compare = std::less<Section>, _Alloc = std::allocator<Section>]’ 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_set.h:408:29: 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 value_type&) [with _Key = Section, _Compare = std::less<Section>, _Alloc = std::allocator<Section>, typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator = std::_Rb_tree_const_iterator<Section>, value_type = Section]’ 
plusconfig.cpp:78:38: instantiated from here 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.3/include/g++-v4/bits/stl_function.h:230:22: error: no match for ‘operator<’ in ‘__x < __y’ 

,但我不超載運營< ,我無法弄清楚這是什麼意思....

謝謝,

朱利安。

回答

1

那麼,你需要一種比較你放入std :: set的東西的方法。如果你沒有明確地給出一個,模板期望你的對象實例的'<'以某種方式可用;例如通過重載所述運營商。我認爲這是你的代碼中的問題。我注意到,在某些情況下,您會執行一組指針,並在其他對象中執行。對於指針'<'進行了定義,對於需要照顧的對象。

+0

請注意,指針的'<'可能不會做你想要的。 – 2012-02-17 03:44:45

0

,但我不超載運營<,我想不通這是什麼意思....

這就是爲什麼。

stl::setstl::set<Key, Compare, Alloc>,與Compare默認爲less<Key>

這需要你的類要少那麼相媲美。如果您無法覆蓋<運算符,則可以提供比較功能。