使用 boost::program_options 解析 LPTSTR* 命令行参数

发布于 2024-10-15 01:20:11 字数 2003 浏览 3 评论 0原文

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

解析器构造函数应该采用 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 编译器

I am having a problem with command line parsing with boost:program_options. The quickest way to explain it is to show the code so:

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);

The parser ctor is supposed to take a const std::vector<charT>

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 in my program is

typedef std::basic_string<TCHAR> tstring;

The error I get is:

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

Where, oh where, am I going astray? I've tried all kinds of casting and re-defining, but nothing has worked and I'm at the end of my tether.

Edit @Zac:
Making the changes you suggested... I get the error:

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

Edit
Just to point out that I am using Visual Studio 2008 VC9 compiler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

趁年轻赶紧闹 2024-10-22 01:20:11

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

po::wcommand_line_parser parser(args);

或更灵活:

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

You seem to be using a unicode build, so either explicitly use the wide char version:

po::wcommand_line_parser parser(args);

or the more flexible:

po::basic_command_line_parser<TCHAR> parser(args);
深者入戏 2024-10-22 01:20:11

您误入歧途的行如下:

const std::vector<tstring> args;

将其更改为:

std::vector<tstring> args;

The line you went astray with is below:

const std::vector<tstring> args;

Change it to:

std::vector<tstring> args;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文