Domanda

So, I've got this code:

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

}

which can't be compiled because:

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’

but I'm not overloading operator <, and I can't figure out what this means....

Thanks,

Julian.

È stato utile?

Soluzione

Well, you need a way of comparing stuff that you put in a std::set. If you don't give one explicitly, the template expects that '<' over instances of your objects be available somehow; like by overloading said operator. I think that's the problem in your code. I noticed that in some cases you do sets of pointers, and in others of objects. For pointers '<' is defined, for objects you need to take care of it.

Altri suggerimenti

but I'm not overloading operator <, and I can't figure out what this means....

That's why.

stl::set is stl::set<Key, Compare, Alloc>, with Compare default to less<Key>

This require your class to be less then comparable. You provide a compare function if you can't override < operator.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top