我正在使用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