【问题标题】:Unrecognized error in boost library升压库中无法识别的错误
【发布时间】:2014-04-11 06:43:50
【问题描述】:

我从一本书 (Boost C++ Application Development Cookbook) 中获得了这段代码,我尝试运行它,它使用 g++ 编译,但是当我尝试运行它时,它给了我这个错误“在抛出 'boost: :exception_detail::clone_impl >' what():未知的选项范围 中止(核心转储)“ 它没有用clang编译, 我用“g++ ex1.cxx -lboost_program_options”和“clang ex1.cxx -lboost_program_options”编译

#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
#include <iostream>

namespace opt = boost::program_options;

int main(int argc, char* argv[]){
    opt::options_description desc("All options");
    // 'a' and 'o' are short option names for apples
    desc.add_options()("apple,a", opt::value<int>()->default_value(10),
        "apples that you have")
        ("oranges,o", opt::value<int>(), "oranges that you have")("name",
        opt::value<std::string>(), "your name")("help", "produce help message");
    opt::variables_map vm;
    opt::store(opt::parse_command_line(argc, argv, desc), vm);
    opt::notify(vm);
    if(vm.count("help")){
        std::cout << desc << "\n";
        return 1;
    }
    try{
        opt::store(opt::parse_config_file<char>("apples_oranges.cfg", desc), vm);
    }
    catch(const opt::reading_file& e){
        std::cout << "Failed to open 'apples_oranges.cfg': " << e.what();
    }
    opt::notify(vm);
    if(vm.count("name")){
        std::cout << "Hi, "<< vm["name"].as<std::string>() << "!\n";
    }
    std::cout << "Fruits count: "
        << vm["apples"].as<int>() + vm["oranges"].as<int>() << std::endl;
    return 0;
}

【问题讨论】:

  • 您介意缩进代码吗?你能说出异常是在哪里抛出的吗?
  • 我缩进了,try and catch 现在很清楚了
  • 非常感谢。这更好读。能说一下异常是在哪里抛出的吗?
  • apples_oranges.cfg的内容是什么?还是没有达到?
  • 代码没有抛出,只是尝试和捕获,错误没有给出任何行号

标签: c++ boost boost-program-options


【解决方案1】:

这里有错别字

vm["apples"].as<int>();

应该是

vm["apple"].as<int>();

Live On Coliru

#include <boost/program_options.hpp>
#include <boost/program_options/errors.hpp>
#include <iostream>

namespace opt = boost::program_options;

int main(int argc, char* argv[]){
    opt::options_description desc("All options");
    // 'a' and 'o' are short option names for apples
    desc.add_options()
        ("apple,a",   opt ::value<int>()->default_value(10), "apples that you have")
        ("oranges,o", opt ::value<int>(),                    "oranges that you have")
        ("name",      opt ::value<std::string>(),            "your name")
        ("help", "produce help message");

    opt::variables_map vm;
    opt::store(opt::parse_command_line(argc, argv, desc), vm);

    opt::notify(vm);
    if(vm.count("help")){
        std::cout << desc << "\n";
        return 1;
    }
    try{
        opt::store(opt::parse_config_file<char>("apples_oranges.cfg", desc), vm);
    }
    catch(const opt::reading_file& e){
        std::cout << "Failed to open 'apples_oranges.cfg': " << e.what();
    }
    opt::notify(vm);
    if(vm.count("name")){
        std::cout << "Hi, "<< vm["name"].as<std::string>() << "!\n";
    }
    int a_ = vm["apple"].as<int>();
    int o_ = vm["oranges"].as<int>();
    std::cout << "Fruits count: " << a_ + o_ << std::endl;
}

【讨论】:

    猜你喜欢
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 2018-12-16
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多