我正在使用boost :: program_options并希望将“域”类选项传递给我的程序。这样的域名是:如何重载operator <<用于boost :: program_options默认值输出?
template<typename T>
struct CDomain
{
CDomain(T min = -1, T max = 1) {_min = min; _max = max;};
T _min;
T _max;
};
我已经为这个选项类编写了一个自定义验证器,并且运行良好。现在,我想在
desc.add_options()("domain", po::value<CDomain<long double> >()->default_value(CDomain<long double>(-1,1)), "domain");
的boost :: program_options呼吁运营商< <添加一个默认值,如:
error: no match for ‘operator<<’ in ‘stream << input’
我加入这一个,但仍然得到同样的错误信息:
template<typename T>
ostream& operator<<(ostream& o, CDomain<T>& d)
{
return o << "[" << boost::lexical_cast<string>(d._min) << ":" << boost::lexical_cast<string>(d._max) << "]";
}
我如何定义操作< <与自定义选项说明默认值输出用?
我做了一些更多的调查。在boost/lexical_cast.hpp:1147 In member function bool boost::detail::lexical_stream_limited_src<CharT, Traits, RequiresStringbuffer>::shl_input_streamable(InputStreamable&) [with InputStreamable = const CDomain<long double>, CharT = char, Traits = std::char_traits<char>, bool RequiresStringbuffer = true]
出现的错误:
template<typename InputStreamable>
bool shl_input_streamable(InputStreamable& input)
{
std::basic_ostream<CharT> stream(&stringbuffer);
bool const result = !(stream << input).fail();
start = stringbuffer.pbase();
finish = stringbuffer.pptr();
return result && (start != finish);
}
这可能是一个命名空间的问题,但是移动ostream& operator<<(ostream& o, CDomain<T>& d)
到的boost ::细节并没有解决问题。
运营商的工作原理与我所定义的一样 - 我可以随时拨打电话使用它。问题是,提振希望运营商<<,但没有找到我的。 – Christoph 2012-03-02 11:33:26
你有没有试过这种方式,看看它是否有所作为?你在哪里定义了你的'operator <<'函数?它是在你想要使用它的地方之前定义的? – 2012-03-02 12:24:24
我试过它没有成功,但现在写了最小的代码来再次测试它,一切都很顺利。谢谢!我只是不明白为什么非朋友类外运营商“没有工作...... – Christoph 2012-03-02 12:31:07