【问题标题】:Parsing LPTSTR* command line args with boost::program_options使用 boost::program_options 解析 LPTSTR* 命令行参数
【发布时间】:2011-02-01 12:11:46
【问题描述】:

我在使用 boost:program_options 进行命令行解析时遇到问题。解释它的最快方法是显示代码:

const std::vector<tstring> args;
if (ac > 0 && NULL!=av) //ac is a ULONG
{
    for (int i = 0; i < ac; i++) 
    {
        args.push_back(av[i]); //av is an LPTSTR pointer (pointer to TCHAR*)
    }

    }
    po::command_line_parser parser(args);

解析器 ctor 应该采用 const std::vector

typedef basic_command_line_parser<char> command_line_parser;
typedef basic_command_line_parser<wchar_t> wcommand_line_parser;

/** Creates instance of 'command_line_parser', passes parameters to it,
    and returns the result of calling the 'run' method.        
 */
template<class charT>
    class basic_command_line_parser : private detail::cmdline {
    public:
        /** Creates a command line parser for the specified arguments
            list. The 'args' parameter should not include program name.
        */
        basic_command_line_parser(const std::vector<
                                  std::basic_string<charT> >& args);

我程序中的tstring是

typedef std::basic_string<TCHAR> tstring;

我得到的错误是:

Error   16  error C2664: 'boost::program_options::basic_command_line_parser<charT>::basic_command_line_parser(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'const std::vector<_Ty>' to 'const std::vector<_Ty> &'   myfile.cpp  329

哪里,哦哪里,我会误入歧途吗?我尝试了各种类型的转换和重新定义,但没有任何效果,我已经走到了尽头。

编辑@Zac:
进行您建议的更改...我收到错误:

Error   14  error C2664: boost::program_options::basic_command_line_parser<charT>::basic_command_line_parser(const std::vector<_Ty> &)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'const std::vector<_Ty> &'  MyFile.cpp  328

编辑 只是指出我正在使用 Visual Studio 2008 VC9 编译器

【问题讨论】:

    标签: c++ string boost boost-program-options


    【解决方案1】:

    您似乎正在使用 unicode 构建,因此要么明确使用宽字符版本:

    po::wcommand_line_parser parser(args);
    

    或更灵活的:

    po::basic_command_line_parser<TCHAR> parser(args);
    

    【讨论】:

    • 就是乔治。我将 typedef 一个 tcommand_line_parser。谢谢大佬。
    【解决方案2】:

    你误入歧途的路线如下:

    const std::vector<tstring> args;
    

    改成:

    std::vector<tstring> args;
    

    【讨论】:

    • @Dennis:尽管解析器 ctor 通过 const& 获取向量,但这并不意味着您传递给它的向量必须是 const。这确实意味着 ctor 不会更改向量。这就是为什么这个答案会删除您不必要的 const,当您稍后尝试通过 push_back 修改向量时,这是一个错误。
    • 弗雷德...准时。扎克……没用。这是我的程序的第一次迭代。请参阅有问题的编辑帖子。
    • @Dennis:检查以确保您在项目设置中关闭了 UNICODE 构建选项。
    • 这不取决于我...我们有 unicode 和非 unicode 构建(不知道为什么我们仍然有非 unicode 构建)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2021-11-05
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多