C++ 如何让Boost.LexicalCast开始工作?

C++ 如何让Boost.LexicalCast开始工作?,c++,boost,c++11,lexical-cast,C++,Boost,C++11,Lexical Cast,我对boost::lexical\u cast有问题。我试图在GLM(OpenGL数学)库中的一个类上使用它 为了实现词法转换,我实现了操作符vec.y; 返回; } 我对操作员进行了如下测试: std::cout << glm::ivec2(1, 1) << glm::vec2(1.0f, 1.0f); std::cout pt; std::cout(InputStreamable&)[带有InputStreamable=glm::detail::tvec2,Ch

我对boost::lexical\u cast有问题。我试图在GLM(OpenGL数学)库中的一个类上使用它

为了实现词法转换,我实现了操作符vec.y; 返回; } 我对操作员进行了如下测试:

std::cout <<  glm::ivec2(1, 1) << glm::vec2(1.0f, 1.0f);
std::cout pt;
std::cout(InputStreamable&)[带有InputStreamable=glm::detail::tvec2,CharT=char,Base=std::basic\u streambuf,Traits=std::char\u Traits]:
/usr/include/boost/lexical_cast.hpp:1151:13:从“Target boost::detail::lexical_cast”实例化(typename boost::call_traits::param_type,CharT*,std::size_t)[Target=glm::detail::tvec2,Source=const char*,bool Unlimited=false,CharT=char,typename boost::call_traits::param_type=const char*const,std::size_t=long unsigned int]'
/usr/include/boost/lexical_cast.hpp:1174:77:从“Target boost::lexical_cast(const Source&)[with Target=glm::detail::tvec2,Source=char[8]]实例化”
test2.cpp:41:59:从此处实例化
/usr/include/boost/lexical_cast.hpp:785:29:错误:无法将'std::basic_istream'左值绑定到'std::basic_istream&'
/usr/include/c++/4.6/istream:852:5:错误:初始化'std::basic\u istream&std::operator>>(std::basic\u istream&&,_Tp&)[with _CharT=char,_Traits=std::char\u Traits,_Tp=glm::detail::tvec2]'

编辑:似乎只有在包含Boost.PropertyTree的标题时才会发生错误。

您发布了此提取运算符:

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
模板
std::istream&operator>>(std::istream&in,glm::detail::tvec2&vec)
但该错误意味着它正试图使用以下内容进行编译:

std::istream& std::operator>>(std::istream&&, _Tp&)
[with ... _Tp = glm::detail::tvec2<int>]
std::istream&std::operator>>(std::istream&&,_-Tp&)
[带…_Tp=glm::detail::tvec2]
您确定您发布的代码与您正在编译的代码匹配吗? (如果不能立即看到,请将注意力集中在运算符的第一个参数上)


好的,这个完整的代码对我来说很好:

#include <sstream>
#include <iostream>

#include <boost/lexical_cast.hpp>

template <class T>
struct tvec2 { 
    tvec2() : x(), y() {}
    tvec2(tvec2 const &) = default;
    tvec2(T x_, T y_) : x(x_), y(y_) {}

    T x;
    T y;
};

template <class T>
std::ostream& operator<<(std::ostream& out, const tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, tvec2<T>& vec)
{
    // yuck, boost disables skipws on the input stream
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

void test_operators()
{
    tvec2<int> intvec(2,3);
    std::cout << "intvec = {" << intvec << "}\n";

    std::stringstream ss;
    ss << intvec;

    tvec2<int> dupvec;
    ss >> dupvec;
    std::cout << "dupvec = {" << dupvec << "}\n";
}

void test_lexical_cast()
{
    std::cout << "and now with lexical_cast ...\n";
    tvec2<int> dupvec = boost::lexical_cast<tvec2<int> >("2 3");
    std::cout << "dupvec = {" << dupvec << "}\n";
}

int main()
{
    test_operators();
    test_lexical_cast();
}
#包括
#包括
#包括
模板
结构tvec2{
tvec2():x(),y(){}
tvec2(tvec2常数&)=默认值;
tvec2(tx_u,tyuu):x(x_u),y(y_u){
tx;
T y;
};
模板
标准::ostream&operator>vec.y;
返回;
}
无效测试_运算符()
{
tvec2-intvec(2,3);

std::cout答案是另一个人找到的,所以我把它留在这里

他提到了一些与参数相关的查找,并建议将流运算符放在glm::detail名称空间中,如下所示:

namespace glm {
namespace detail {

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
    out << vec.x << " " << vec.y;
    return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

}} // glm::detail
名称空间glm{
名称空间详细信息{
模板
标准::ostream&operator>vec.y;
返回;
}
}}//glm::detail

在那之后,一切正常。

是的,我绝对确定我发布的代码就是我试图编译的。好的……您是否有使用提取的工作代码(
>
)运算符外部
boost::lexical\u cast
?您只显示测试插入运算符的代码。我将其添加到了我的原始帖子中。好的,我尝试了类似的操作,但遇到了不同的问题(它编译得很好,但我看到了一个错误的\u lexical\u cast异常)。您使用的是什么版本的boost?完整的、或多或少可以工作的最小代码(对我来说)答案中添加了GCC4.4.2和Boost1.47.0,您能试试看得到什么吗?
template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
std::istream& std::operator>>(std::istream&&, _Tp&)
[with ... _Tp = glm::detail::tvec2<int>]
#include <sstream>
#include <iostream>

#include <boost/lexical_cast.hpp>

template <class T>
struct tvec2 { 
    tvec2() : x(), y() {}
    tvec2(tvec2 const &) = default;
    tvec2(T x_, T y_) : x(x_), y(y_) {}

    T x;
    T y;
};

template <class T>
std::ostream& operator<<(std::ostream& out, const tvec2<T>& vec)
{
  out << vec.x << " " << vec.y;
  return out;
}

template <class T>
std::istream& operator>>(std::istream& in, tvec2<T>& vec)
{
    // yuck, boost disables skipws on the input stream
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

void test_operators()
{
    tvec2<int> intvec(2,3);
    std::cout << "intvec = {" << intvec << "}\n";

    std::stringstream ss;
    ss << intvec;

    tvec2<int> dupvec;
    ss >> dupvec;
    std::cout << "dupvec = {" << dupvec << "}\n";
}

void test_lexical_cast()
{
    std::cout << "and now with lexical_cast ...\n";
    tvec2<int> dupvec = boost::lexical_cast<tvec2<int> >("2 3");
    std::cout << "dupvec = {" << dupvec << "}\n";
}

int main()
{
    test_operators();
    test_lexical_cast();
}
namespace glm {
namespace detail {

template <class T>
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec)
{
    out << vec.x << " " << vec.y;
    return out;
}

template <class T>
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec)
{
    in >> vec.x;
    if (in.good() && in.ignore(256, ' ').good())
        in >> vec.y;
    return in;
}

}} // glm::detail