How to pass a string to “validation_error” method

2019-07-24 02:12发布

问题:

I'm trying to compile a program released by Dalal and Triggs which uses the Boost libraries. I got an error in the Boost method validation_error due to the differences between version which the authors used (1.35) and the one that I'm using (1.46).

In the old version, the validation_error method used by the authors had the following structure:

validation_error(const std::string & what);

And the version of Boost I'm running has the following:

validation_error(kind_t kind, const std::string & option_value = "", 
                 const std::string & option_name = "");

In the code, the authors are passing a string to the old validation_error method (example below).

std::ostringstream ost;
ost << "value " << *value
      << " greater than max value " << max;
throw po::validation_error(ost.str());

How can I pass this string to the new version of validation_error?

回答1:

You could do something like

throw boost::program_options::validation_error(
    boost::program_options::validation_error::invalid_option_value,
    "option name",
    *value
);

or

throw boost::program_options::invalid_option_value(ost.str());


标签: c++ boost