gpt4 book ai didi

c++ - 编译但得到 'uncaught exception of type std::bad_cast'

转载 作者:行者123 更新时间:2023-12-01 14:38:08 24 4
gpt4 key购买 nike

我正在尝试开始使用 Boost for C++。这是一个用 g++ -Wall test.cpp/usr/local/Cellar/boost/1.55​​.0/lib/libboost_locale-mt.a 编译的小程序。

但是,当我运行它时,出现以下错误:libc++abi.dylib:以 std::bad_cast 类型的未捕获异常终止:std::bad_cast
中止陷阱: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())

根据语言环境 loc 将以 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É

关于c++ - 编译但得到 'uncaught exception of type std::bad_cast',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19960496/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com