C++使用模板增强 lexical_cast ?

发布于 2024-11-15 18:35:11 字数 1061 浏览 2 评论 0原文

我正在尝试构建一个将程序设置存储为 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 时,错误输出很长,我不太确定它出了什么问题。

I'm trying to build a class that stores program settings as a std::map. Since all the program settings are stored as strings I'd like an accessor method that can return the program setting casted to the relevant type. I'm new to templating in C++ and this is my first attempt:

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_;

};  

However, I'm getting a compiler errors:

...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

With boost the error output is very long and I'm not really sure what's wrong with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-11-22 18:35:11

CString 没有任何运算符<<
考虑使用 std::string

CString does not have any operator<<
Consider using std::string

圈圈圆圆圈圈 2024-11-22 18:35:11

二进制 '>>' : 没有找到哪个操作员
接受类型的左操作数
'std::basic_istream<_Elem,_Traits>'

lexical_cast 基本上尝试将对象写入流对象。

您需要定义 <<>> 运算符来写入您正在使用的类中的流才能正常工作。 (取决于你是在阅读还是在写作)

binary '>>' : no operator found which
takes a left-hand operand of type
'std::basic_istream<_Elem,_Traits>'

lexical_cast basically tries to write the object into a stream object.

you need << and >> operator defined to write to a stream in the class you're using for it to work. (depends if you're reading or writing)

紫瑟鸿黎 2024-11-22 18:35:11

文档 所示,boost::lexical_cast 确实它的转换基于几个事物的存在。源类型必须有一个运算符<<需要一个 std::ostream (或 std::wostream),并且目标类型必须有一个运算符>>它需要一个 std::istream (或 std::wistream)。这些函数的第一个参数是对流的非常量引用,第二个参数是对要发送/构造的类型的引用。

为了将设置名称转换为 T,该 T 必须具有运算符>>。需要一个输入流。同样,为了转换为CString,必须有一个运算符<<需要一个输出流。

As shown in the documentation, boost::lexical_cast does its conversion based on the presence of several things. The source type must have an operator<< that takes a std::ostream (or std::wostream), and the destination type must have an operator>> that takes a std::istream (or std::wistream). The first parameter to these function is a non-const reference to the stream, and the second parameter is a reference to the type to send/construct.

In order to convert a setting name to a T, that T must have an operator>> that takes an input stream. Similarly, in order to convert to a CString, there must be an operator<< that takes an output stream.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文