如何将字符串传递给 Boost 1.46 中的"validation_error"方法?

How to pass a string to "validation_error" method from Boost 1.46?

本文关键字:validation 中的 error 方法 字符串 Boost      更新时间:2023-10-16

我正在尝试编译Dalal和Triggs发布的一个使用Boost库的程序。我在Boost方法validation_error中遇到了一个错误,因为作者使用的版本(1.35)和我正在使用的版本之间存在差异(1.46)。

在旧版本中,作者使用的validation_error方法具有以下结构:

validation_error(const std::string & what);

我运行的Boost版本有以下内容:

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

在代码中,作者将string传递给旧的validation_error方法(下面的示例)。

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

如何将此string传递到新版本的validation_error

你可以做一些类似的事情

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

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