我的代码不起作用:
wstring config_file;
// Declare a group of options that will be
// allowed only on command line
po::options_description generic("Generic options");
generic.add_options()
("help,h","produce help message")
("config,c",po::wvalue<wstring>(&config_file)->default_value(L"DXDrv.cfg"),"name of a file of a configuration.")
;
编译失败,错误:
d:\repo\a4x_ext\minidxdriver\testapp\configparser\boost\lexical_cast.hpp(1096) : error C2039:
'setg': is not a member of'boost::detail::lexical_stream_limited_src<CharT,Base,Traits>'
解决方法
冗长的解释:这是因为program_options中的基础typed_value类型在设置m_default_value_as_text私有成员时尝试从wchar到char进行词法转换.无论出于何种原因,basic_string类型都没有必要的函数来创建正确的模板类型.
幸运的是,typed_value类对default_value和implicit_value有第二个覆盖,它提供了值的字符串表示.这绕过了lexical_cast,修复了问题.就像是:
tvalue< tstring >()->default_value( _T( "output.png" ),"output.png" )