std :: set weird<过载错误

时间:2012-02-17 03:16:09

标签: c++ stdset

所以,我有这个代码:

#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"));

}

无法编译,因为:

julian@vanaheimr ~/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’

但我没有重载运算符&lt;,我无法弄清楚这意味着什么......

谢谢,

儒略。

2 个答案:

答案 0 :(得分:1)

嗯,你需要一种比较放在std :: set中的东西的方法。如果您没有明确指定,模板需要'&lt;'以某种方式提供对象的实例;喜欢超载所述运营商。我认为这是你代码中的问题。我注意到在某些情况下你会做一组指针,而在其他情况下你会做对象。对于指针'&lt;'已定义,对于您需要处理它的对象。

答案 1 :(得分:0)

  

但我没有超载运营商&lt;,我无法弄清楚这意味着什么....

这就是原因。

stl::setstl::set<Key, Compare, Alloc>Compare默认为less<Key>

这要求你的班级要低于同等水平。如果您无法覆盖<运算符,则可以提供比较功能。