10

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

Dennis
  • 3,683
  • 1
  • 21
  • 43

2 Answers2

6

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);
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
2

The line you went astray with is below:

const std::vector<tstring> args;

Change it to:

std::vector<tstring> args;
Zac Howland
  • 15,777
  • 1
  • 26
  • 42
  • 2
    @Dennis: Even though the parser ctor takes a vector by const&, that doesn't mean the vector you pass into it needs to be const. It does mean that ctor won't change the vector. That is why this answer removes your unnecessary const, which is an error when you later try to modify the vector via push_back. – Fred Nurk Feb 01 '11 at 13:08
  • Fred...spot on. Zac... no worky. This was the first iteration of my program. See the edit in question post. – Dennis Feb 01 '11 at 13:24
  • @Dennis: check to make sure you the UNICODE build option is turned off in your project settings. – Zac Howland Feb 01 '11 at 13:49
  • It's not up to me... we have unicode and non-unicode builds (not sure why we sill have non-unicode builds) – Dennis Feb 01 '11 at 13:53