c++ - How to solve "boost::bad_any_cast: failed conversion using boost::any_cast" when using boost program options? -
// Use the Boost Program option to read command line and configured file data #include & lt; Boost / program_options.hpp and gt; using namespace std; Using Namespace Boost; Name space po = boost :: program_options; Int main (int argc, char * argv []) {po :: options_description config ("configuration"); Config.add_options () ("IPAddress, i", "IP Address") ("Port, P", "Port"); Po :: variables_map vm; Po :: store (po :: parse_command_line (argc, argv, config), vm); Po :: Notify (VM); Cout & lt; & Lt; "Value \ n"; String Address = (VM ["IP Address"]. & Lt; std :: string & gt; ()). C_str (); String port = (VM ["port"]. As & lt; std: string & gt; ()). C_str (); Cout & lt; & Lt; (Vm ["IPAddress"]. & Lt; string & gt; ()). C_str (); Cout & lt; & Lt; "" & Lt; & Lt; (VM ["Port"] & lt;; string & gt; ()). C_str (); Return 0; } Is the input value somehow unreliable?
Here is the GDB output, it seems that the problem can be solved:
Finish Boost :: exception_detail :: clone_impl
'(): Boost :: bad_any_cast: boost :: any_cast
programmable signals SIGABRT, abort 0x0000003afd835935 in raise () to /lib64/libc.so.6 string address = (vm ["IPAddress"]. As ()) c_str () .; That error occurs; I have tried std :: string and string with the same result.
testboostpo -i 192.168.1.10 -p 5000 is the command line. <
config.add_options () ("IPAddress, i", po :: value & lt; std :: string & gt; (), "IP address") ("Port, P ", Po :: value ()," port "); But the error still occurred.
Can this be a real bug?
You boost :: bad_any_cast to po :: variables_map < See exceptions removed from / code> because the two const char * does not specify a po :: value_semantic type of logic overload, so it should be returned to any std :: String will not be converted to . If you want to convert the value to std :: string , and it is necessary for your application, then use required () value. #include & lt; Boost / program_options.hpp & gt; Name space po = boost :: program_options; Int main (int argc, char * argv []) {po :: options_description config ("configuration"); Config.add_options () ("IPAddress, i", po :: value & lt; std :: string & gt; () - & gt; Required (), "IP address") ("Port, P", PO: : Value & lt; std:: string & gt; () - & gt; required (), "port"); Try {po :: variables_map vm; Po :: store (po :: parse_command_line (argc, argv, config), vm); Po :: Notify (VM); Std :: cout & lt; & Lt; "Value" & lt; & Lt; Std :: endl; Const std :: string address = vm ["IPAddress"]. & Lt; Std :: string & gt; (); Const std :: string port = vm ["port"]. As & lt; Std :: string & gt; (); Std :: cout & lt; & Lt; "Address:" & lt; & Lt; Address & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; "Port:" & lt; & Lt; Harbor & lt; & Lt; Std :: endl; } Hold (const std :: exception & e) {std :: cerr & lt; & Lt; E.what () & lt; & Lt; Std :: endl; Return 1; } Return 0; } Note the added block block since parsing (and as you've noted) Leave Exception Here is a sample session:
Samm $ ./a.out Option '- IPADress' is required, but SAMM $ / A.out is unavailable - IPRAR 127.0.0.1 option requires 'port -' but SAM $ is missing. / A.out - IPadder 127.0.0.1 - Port 5000 Price Address: 127.0.0.1 Port: 5000 SAM $ Here is the courtesy behavior of the compile link RUIN (Koloiru), the same behavior Showing
Comments
Post a Comment