Trouble With Retrieving A std::string From boost::program_options

S

Starfox

The Following Code:

//Setup the options
string InputName, OutputSkeletonName, OutputModelName;
program_options::eek:ptions_description OptionDesc("Available Options");
OptionDesc.add_options()
("help", "produce help message")
("input,I", program_options::value<string>(&InputName), "Input
md5mesh File")
("out-skl,S", program_options::value<string>(&OutputSkeletonName),
"Output Skeleton File")
("out-mdl,M", program_options::value<string>(&OutputModelName),
"Output Model File");
program_options::variables_map OptionMap;
program_options::store(program_options::parse_command_line(argc, argv,
OptionDesc), OptionMap);
if(OptionMap.count("help"))
{
cout << OptionDesc;
return -2;
}
string t = OptionMap["I"].as<const char*>(); //CRASH

Crashes at the last line, even if i replace the const char* with
std::string. I'm using VC++ 2005 on Windows XP SP2 and BOOST 1.32.0 .
Any ideas?
 
M

Mathias Waack

Starfox said:
The Following Code:

//Setup the options
string InputName, OutputSkeletonName, OutputModelName;
program_options::eek:ptions_description OptionDesc("Available Options");
OptionDesc.add_options()
("help", "produce help message")
("input,I", program_options::value<string>(&InputName), "Input

Ok, the Input-Parameter following after "-I" should be taken as an string.
md5mesh File")
("out-skl,S", program_options::value<string>(&OutputSkeletonName),
"Output Skeleton File")
("out-mdl,M", program_options::value<string>(&OutputModelName),
"Output Model File");
program_options::variables_map OptionMap;
program_options::store(program_options::parse_command_line(argc, argv,
OptionDesc), OptionMap);
if(OptionMap.count("help"))
{
cout << OptionDesc;
return -2;
}
string t = OptionMap["I"].as<const char*>(); //CRASH

What do you want to do? You've advised boost to take the parameter for "-I"
as a string, after that you asked the program to read it as an "const
char*" and at least you assign the result to a string.
Crashes at the last line, even if i replace the const char* with
std::string. I'm using VC++ 2005 on Windows XP SP2 and BOOST 1.32.0 .
Any ideas?

Depends on what do you want to do. Ever tried the mucher shorter form:

string t = InputName;

(even if it doesn't make sense for me...)

Mathias
 
S

Starfox

I tried t = InputName, but InputName is completely empty!! it never
gets touched. and like i said, if i change the char* to a string it
still doesn't work. any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top