【问题标题】:UTF-8 locale in Visual C++ 2010Visual C++ 2010 中的 UTF-8 语言环境
【发布时间】:2012-03-21 05:46:29
【问题描述】:

我正在尝试仅使用标准库而不是 Boost 或 Windows API 来读取 Visual C++ 2010 中的 UTF-8 文本文件。我将语言环境定义为:

std::locale utf8_locale(std::locale(), new std::codecvt_utf8<wchar_t>);

但这会导致以下编译器错误:

error C2661: 'std::locale::facet::operator new' : no overloaded function takes 3 arguments
error C2664: 'std::locale::locale(const char *,std::locale::category)' : cannot convert parameter 1 from 'std::locale' to 'const char *'

【问题讨论】:

  • 你必须修复你的代码才不会出现编译错误。
  • 向我们展示编译器错误和更多代码。
  • 我在问题中添加了编译器错误
  • 我使用了您的代码(谢谢!),但后来我意识到我认为您的第二个参数(新的 std::codecvt_utf8)存在内存泄漏。我是 C++ 新手,所以我可能错了
  • 除了你的问题你想使用std::codecvt_utf8_utf16

标签: c++ visual-c++ utf-8


【解决方案1】:

该错误是在调试模式下,当代码用于放置 micrsoft Visual c++ 提供的宏下面的文件中时发生的。

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

要摆脱这个错误,#define new DEBUG_NEW 应该被注释掉,或者代码应该在另一个没有上述宏的文件中实现。 这里提到了 Visual c++ 2010 的这个错误http://connect.microsoft.com/VisualStudio/feedback/details/683483/mfc-c-fails-to-compile-use-of-codecvt-utf8-in-debug-configuration

【讨论】:

  • 这只是 MFC 的问题。除非您使用 MFC,否则不要链接到 MFC 库。是的,它只是为 MFC 库的调试版本定义的。
  • FWIW 和 IIRC,在 C++ 中用保留字的名称定义宏是非法的,例如 new。所以这段代码是 MFC 对 C++ 的又一次滥用。
【解决方案2】:

仅供参考,另一种解决方法是编写

std::locale utf8(std::locale(), ::new std::codecvt_utf8<wchar_t>);

这将强制编译器使用全局 new,而不是 locale new

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 2016-02-16
    • 2011-04-12
    • 2023-04-03
    • 2017-08-10
    • 2014-01-01
    相关资源
    最近更新 更多