命名空间'std'中没有名为 'u16string' 的类型

No type named 'u16string' in namespace 'std'

本文关键字:u16string 类型 std 命名空间      更新时间:2023-10-16

我使用的是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 

修复:

  1. 由于mac上的Qt是由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

有趣的是,我在过去成功地编译了它,为什么它现在失败了?

之前你应该包含一些库的头文件,而这些头文件又包含<string>;这个,在一些更新之后,可能会停止直接包括它,从而出现错误。

如何修复?

在源代码中包含适当的头文件,以避免此类问题。

#include <string>
在发生错误的翻译单元中

,并确保使用命名空间:std::u16string .

引用它。

问题是在文件ExpGame.pro.I删除代码如下:

QMAKE_CXXFLAGS += -std=c++11