【问题标题】:No type named 'u16string' in namespace 'std'命名空间“std”中没有名为“u16string”的类型
【发布时间】:2015-07-17 08:19:34
【问题描述】:

我正在使用 QT 5.5.0。

当我编译一个程序时,它显示“在命名空间 'std' 中没有名为 'u16string' 的类型”。有趣的是我过去编译成功,为什么现在失败了? qstring.h 似乎有问题。

我该如何解决?这是发生错误的地方

#ifndef QSTRING_H 
#define QSTRING_H 
#if defined(QT_NO_CAST_FROM_ASCII) && defined(QT_RESTRICTED_CAST_FROM_ASCII) 
#error QT_NO_CAST_FROM_ASCII and QT_RESTRICTED_CAST_FROM_ASCII must not be defined at the same time 
#endif 

#include <QtCore/qchar.h> 
#include <QtCore/qbytearray.h> 
#include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h> 
#include <string> 
#if defined(Q_OS_ANDROID)
// std::wstring is disabled on android's glibc, as bionic lacks certain features 
// that libstdc++ checks for (like mbcslen). namespace std { typedef basic_string<wchar_t> wstring; }
#endif

#if defined(Q_COMPILER_UNICODE_STRINGS) || defined(Q_QDOC) 
   static inline QString fromStdU16String(const std::u16string &s); 
   inline std::u16string toStdU16String() const; 
   static inline QString fromStdU32String(const std::u32string &s); 
   inline std::u32string toStdU32String() const; 
#endif 

【问题讨论】:

  • 如果您发布代码会很有用,请
  • @PaoloM #如果定义(Q_COMPILER_UNICODE_STRINGS)|| defined(Q_QDOC) 静态内联 QString fromStdU16String(const std::u16string &s);内联 std::u16string toStdU16String() const;静态内联 QString fromStdU32String(const std::u32string &s);内联 std::u32string toStdU32String() const; #endif
  • 你加入了&lt;string&gt;吗?查看更多代码会很有帮助。也许编辑您的问题,而不是将代码作为评论发布。
  • @muXXmit2X 代码太长,大约有 1500 个低点。
  • @shengfuzou 好吧,因为u16string 是在c++ 标头string 中定义的,所以您需要包含该标头。确保在您使用 u16string 的文件中包含 #include &lt;string&gt;

标签: c++ qt


【解决方案1】:

修复它:

  1. 由于Qt on mac是clang编译的,在“Qt Creator”->“Preferences”->“Kits”中,需要将“Compiler”设置为clang。

  2. 不要写“QMAKE_CXXFLAGS += -std=c++11”,而是将以下配置添加到您的 .pro 文件中:

    CONFIG += c++11
    

https://forum.qt.io/topic/56064/solved-problem-with-qt-5-5/11

【讨论】:

    【解决方案2】:

    有趣的是我以前编译成功,为什么现在编译失败了?

    之前你会包含一些库的标题,而这些标题又包含&lt;string&gt;;这可能会在一些更新之后停止直接包含它,因此会出现错误。

    如何解决?

    在您的源代码中包含正确的标题以避免此类问题。

    #include <string>
    

    在发生错误的translation unit 中,并确保您使用命名空间引用它:std::u16string

    【讨论】:

    • 正常情况下会这样,但对于盛福的情况没有帮助。失败出现在 Qt 库本身的一个名为 qstring.h 的头文件中。在他自己的所有头文件中包含 不会改变任何东西。有趣的是, 包含在错误的头文件中(如复制到问题中的代码所示),但是当引用 std::u16string 时问题仍然存在(也显示在代码中)。我不确定真正的原因是什么,因为我自己也在努力解决同样的问题。
    【解决方案3】:

    问题出在ExpGame.pro文件中。我删除代码如下:

    QMAKE_CXXFLAGS += -std=c++11

    没关系。

    【讨论】:

    • 这将删除对 C++11 的支持,因此您将无法使用任何 C++11 功能。我觉得这不是一个好的最终解决方案。
    猜你喜欢
    • 2014-08-18
    • 1970-01-01
    • 2014-09-21
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 2013-05-27
    • 2012-11-08
    相关资源
    最近更新 更多