boost :: lexical_cast无法识别重载的istream运算符 [英] boost::lexical_cast not recognizing overloaded istream operator
问题描述
我有以下代码:
#include <iostream>
#include <boost\lexical_cast.hpp>
struct vec2_t
{
float x;
float y;
};
std::istream& operator>>(std::istream& istream, vec2_t& v)
{
istream >> v.x >> v.y;
return istream;
}
int main()
{
auto v = boost::lexical_cast<vec2_t>("1231.2 152.9");
std::cout << v.x << " " << v.y;
return 0;
}
我从Boost收到以下编译错误:
I am receiving the following compile error from Boost:
错误1错误C2338:目标类型都不是std :: istream
able nor std::wistream
able
这似乎很简单,在过去的一个小时里,我一直把头撞在桌子上.任何帮助将不胜感激!
This seems straightforward enough, and I have been hitting my head against the desk for the last hour. Any help would be appreciated!
编辑:我正在使用Visual Studio 2013.
I am using Visual Studio 2013.
推荐答案
正在进行两阶段查找.
您需要使用ADL启用过载,因此lexical_cast
将在第二阶段找到它.
You need to enable the overload using ADL, so lexical_cast
will find it in the second phase.
因此,您应该将重载移至名称空间mandala
So, you should move the overload into namespace mandala
这是一个完全固定的示例(您还应该使用std::skipws
):
Here's a completely fixed example (you should also use std::skipws
):
#include <iostream>
#include <boost/lexical_cast.hpp>
namespace mandala
{
struct vec2_t {
float x,y;
};
}
namespace mandala
{
std::istream& operator>>(std::istream& istream, vec2_t& v) {
return istream >> std::skipws >> v.x >> v.y;
}
}
int main()
{
auto v = boost::lexical_cast<mandala::vec2_t>("123.1 15.2");
std::cout << "Parsed: " << v.x << ", " << v.y << "\n";
}
这篇关于boost :: lexical_cast无法识别重载的istream运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!