Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to solve "boost::bad_any_cast: failed conversion using boost::any_cast" when using boost program options?
    primarykey
    data
    text
    <pre><code>//Using boost program options to read command line and config file data #include &lt;boost/program_options.hpp&gt; using namespace std; using namespace boost; namespace 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; "Values\n"; string address = (vm["IPAddress"].as&lt;std::string &gt;()).c_str(); string port = (vm["Port"].as&lt;std::string&gt;()).c_str(); cout &lt;&lt; (vm["IPAddress"].as&lt; string &gt;()).c_str(); cout &lt;&lt; " " &lt;&lt; (vm["Port"].as&lt;string&gt;()).c_str(); return 0; } </code></pre> <p>Are the inputted values somehow unprintable?</p> <p>Here is gdb output, seems to be be cast problem:</p> <blockquote> <p>terminate called after throwing an instance of 'boost::exception_detail::clone_impl</p> <blockquote> <p>' what(): boost::bad_any_cast: failed conversion using boost::any_cast</p> </blockquote> <pre><code> Program received signal SIGABRT, Aborted. 0x0000003afd835935 in raise () from /lib64/libc.so.6 </code></pre> </blockquote> <pre><code>string address = (vm["IPAddress"].as&lt;std::string &gt;()).c_str(); </code></pre> <p>is where the error occurs; I have tried std::string and string with the same results.</p> <pre><code>testboostpo -i 192.168.1.10 -p 5000 </code></pre> <p>is the command line.</p> <p>I tried declaring the types, like so:</p> <pre><code>config.add_options() ("IPAddress,i", po::value&lt;std::string&gt;(), "IP Address") ("Port,p", po::value&lt;std::string&gt;(), "Port"); </code></pre> <p>but the error still occurred.</p> <p>Could this be a genuine bug?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload