だから、私はこのコードを持っています:
#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’
しかし、私は演算子<をオーバーロードしておらず、これが何を意味するのか理解できません。
ありがとう、
ジュリアン。