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

6
我正在使用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
如果您能发布您的代码,那将非常有用。 - Paolo M
@PaoloM #如果定义了(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

- shengfu zou
你是否已经包含了<string>?看到更多的代码会更有帮助。也许可以编辑你的问题,而不是将代码作为评论发布。 - muXXmit2X
#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和QT_RESTRICTED_CAST_FROM_ASCII不能同时定义 #endif#include <QtCore/qchar.h> #include <QtCore/qbytearray.h> #include <QtCore/qrefcount.h> #include <QtCore/qnamespace.h>#include <string>#if defined(Q_OS_ANDROID) // 在Android的glibc上禁用std::wstring,因为bionic缺少某些libstdc++检查的功能(如mbcslen)。 namespace std { typedef basic_string<wchar_t> wstring; } #endif - shengfu zou
@ecatmur + demonplus:我在使用OSX 10.10.5(Yosemite)、Qt Creator 3.6.0(IDE)、Clang 6.0 64位(编译器)、Qt 5.5.0/5.5.1、QMake和C++14时遇到了同样的问题。但是,当我使用Windows 7和MingW+GCC时,就没有这个问题。有趣的是,当我使用C++11(shengfu正在使用)时,编译没有问题,但这会导致另一个可能无关的问题:http://stackoverflow.com/questions/33243888/why-cant-clang-find-the-project-directory-for-this-qt-project-when-building-on - Alex Jansen
显示剩余5条评论
3个回答

7

0
有趣的是,我以前成功编译过它,为什么现在失败了?
早些时候,您可能已经包含了一些库的头文件,其中又包含了;这可能在某次更新后停止直接包含它,从而导致错误。
如何修复它?
在您的源代码中包含正确的头文件以避免这些问题。
#include <string>

在出现错误的翻译单元中,确保您使用命名空间引用它:std :: u16string

1
通常情况下这应该是有效的,但对shengfu来说并没有帮助。失败在Qt库本身的一个头文件qstring.h中。在他自己的所有头文件中包含<string>不会改变任何东西。有趣的是,在有问题的头文件中包含了<string>(如问题中复制的代码所示),但当引用std::u16string时问题仍然存在(也显示在代码中)。我不确定真正的原因是什么,因为我自己也在努力解决同样的问题。 - Alex Jansen

-1
问题出在ExpGame.pro文件中。我删除了以下代码:
QMAKE_CXXFLAGS += -std=c++11
然后就没问题了。

这将移除对C++11的支持,因此您将无法使用任何C++11功能。我认为这不是一个好的最终解决方案。 - Alex Jansen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接