2012-02-17 3 views
0

그래서, 나는이 코드를 가지고 <이상한 설정 :표준 : 과부하 오류

#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>입니다. 연산자 <을 무시할 수없는 경우 비교 기능을 제공합니다.