簡體   English   中英

編譯但得到“類型為std :: bad_cast的未捕獲異常”

[英]Compiles but getting 'uncaught exception of type std::bad_cast'

我正在嘗試使用Boost for C ++。 這是一個用g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a編譯的小程序g++ -Wall test.cpp /usr/local/Cellar/boost/1.55.0/lib/libboost_locale-mt.a

但是,當我運行它時,這是我得到的錯誤: libc++abi.dylib: terminating with uncaught exception of type std::bad_cast: std::bad_cast Abort trap: 6

#include <string>
#include <iostream>
#include <boost/locale.hpp>

int main(void) {
    char test[] = "Variété";
    boost::locale::to_upper(test);
    std::cout << test << std::endl;
    return 0;
}

這可能是什么原因? 謝謝!

我在Mac OSX Mavericks上。

根據文檔:

http://www.boost.org/doc/libs/1_48_0/libs/locale/doc/html/group__convert.html#ga7889a57e1bc1059fbb107db0781d0b6d

    std::basic_string<CharType> boost::locale::to_lower(CharType const *str,
                                   std::locale const &loc = std::locale())

根據區域設置將NUL終止的字符串str轉換為小寫

注意: 如果loc沒有安裝轉換器方面,則拋出std :: bad_cast

因此,這解決了我機器上的問題。

#include <string>
#include <iostream>
#include <boost/locale.hpp>

int main(void) {
    std::string test = "Variété";
    std::locale loc = boost::locale::generator().generate("en_US.UTF-8");
    std::string test_u = boost::locale::to_upper(test, loc);
    std::cout << test << " -> " << test_u << std::endl;
    return 0;
}

輸出:

Variété -> VARIÉTÉ

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM