10

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

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 编译器

4

2 回答 2

6

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

po::wcommand_line_parser parser(args);

或更灵活:

po::basic_command_line_parser<TCHAR> parser(args);
于 2011-02-01T13:35:38.647 回答
2

您误入歧途的路线如下:

const std::vector<tstring> args;

将其更改为:

std::vector<tstring> args;
于 2011-02-01T12:19:33.117 回答