2014-04-11 2 views
1

책 (부스트 C++ 응용 프로그램 개발 요리 책)에서이 코드를 얻었고 g ++로 컴파일했지만 실행 해보면이 오류가 발생합니다. 예 : 'boost :: exception_detail :: clone_impl>' what() : 알 수없는 옵션 범위 중단됨 (코어 덤프 됨) clang 함께 컴파일되지 않았습니다. "g ++ 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; 
} 
+0

를 참조해야 하는가? 예외가 던져지는 곳을 알 수 있습니까? –

+0

내가 들여 쓰기 했으므로 지금 시도 및 캐치가 확실 함 – JSamson

+0

고마워요. 읽는 것이 더 좋습니다. 예외가 던져지는 곳을 말할 수 있습니까? –

답변

0

여기 오타

,536가 ex1.cxx -lboost_program_options "
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; 
} 
+0

그냥 알았는데 지금은 고맙습니다. ! – JSamson