我正在尝试构建一个将程序设置存储为 std::map 的类。由于所有程序设置都存储为字符串,我想要一个可以返回转换为相关类型的程序设置的访问器方法。我是 C++ 模板的新手,这是我的第一次尝试:
class Settings
{
public:
Settings(void);
virtual ~Settings(void);
enum SettingName {HomePageUrl, WindowWidth};
template<class T>
T Get(SettingName name)
{
return boost::lexical_cast<T>(settings_[name]);
}
template<class T>
void Set(SettingName name, T value)
{
settings_[name] = boost::lexical_cast<CString>(value);
}
private:
std::map<SettingName, CString> settings_;
};
但是,我收到编译器错误:
...boost\boost_1_46_1\boost\lexical_cast.hpp(776): error C2678: binary '>>' :
no operator found which takes a left-hand operand of type
'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
..settings.h(33) : see reference to function template instantiation
'Target boost::lexical_cast<CString,T>(const Source &)' being compiled
随着 boost 的错误输出很长,我不确定它有什么问题。