2012-02-17 15 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

ポインタのための '<'は、おそらくあなたが望むことをしないことに注意してください。 –

0

が、私はオペレータ<をオーバーロードていないよ、と私はこれが何を意味するのかを把握することはできません....理由です

stl::setはこれに匹敵その後、小さくなるように、あなたのクラスを必要とless<Key>

Compareデフォルトで、stl::set<Key, Compare, Alloc>です。 <演算子を上書きできない場合は、比較機能を提供します。

関連する問題