6

I'm using QT 5.5.0.

When I compile a program, it shows “no type named 'u16string' in namespace 'std'”. The interesting part is that I compiled it successfully in the past, why is it failing now? It seems to be trouble with qstring.h.

How do I fix it? Here is where the error happen

#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 
muXXmit2X
  • 2,745
  • 3
  • 17
  • 34
shengfu zou
  • 560
  • 1
  • 7
  • 16
  • It would be useful if you post your code, please – Paolo M Jul 17 '15 at 08:22
  • @PaoloM #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 – shengfu zou Jul 17 '15 at 08:24
  • Have you included ``? It would be helpfull to see more code. Maybe edit your question instead of posting the code as a comment. – muXXmit2X Jul 17 '15 at 08:30
  • @muXXmit2X the code is too long,which has about 1500 lows. – shengfu zou Jul 17 '15 at 08:38
  • @shengfuzou Well, as `u16string` is defined in the c++ header `string` you need to include that header. Be sure to have the line `#include ` in the file where you use `u16string` – muXXmit2X Jul 17 '15 at 08:41
  • @ecatmur it should be clang – shengfu zou Jul 17 '15 at 08:43
  • @muXXmit2X yes, I have – shengfu zou Jul 17 '15 at 08:43
  • @shengfuzou Have you enabled the c++11 features while compiling? I guess using clang it was something like `-std=c++11` – muXXmit2X Jul 17 '15 at 08:56
  • #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 #include #include #include #include #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 wstring; } #endif – shengfu zou Jul 17 '15 at 08:58
  • @ecatmur + demonplus: I'm experiencing this same issue when using OSX 10.10.5 (Yosemite), Qt Creator 3.6.0 (IDE), Clang 6.0 64-bit (compiler), Qt 5.5.0/5.5.1, QMake, and C++14. There is no issue when I use Windows 7 and MingW+GCC. Interestingly, there is no issue compiling when I use C++11 (which shengfu was using), but this leads to another issue which is probably unrelated: http://stackoverflow.com/questions/33243888/why-cant-clang-find-the-project-directory-for-this-qt-project-when-building-on – Alex Jansen Oct 22 '15 at 20:33

3 Answers3

7

To fix it:

  1. as the Qt on mac is built by clang, in "Qt Creator" -> "Preferences" -> "Kits", you should set "Compiler" to clang.

  2. instead of writing "QMAKE_CXXFLAGS += -std=c++11", add below config to your .pro file:

    CONFIG += c++11
    

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

xwl
  • 840
  • 10
  • 12
0

The interesting part is that I compiled it successfully in the past, why is it failing now?

Earlier you would have included some library's header which in turn included <string>; this, after some update perhaps, would've stopped including it directly and thus the error.

How to fix it?

Include the proper header in your source to avoid these kind of issues.

#include <string>

in the translation unit where the error occurs and make sure you're referring to it with the namespace: std::u16string.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • 1
    Normally this would work, but it doesn't help in shengfu's case. The failure is in the Qt library itself in a header file called qstring.h. Including in all of his own header files won't change anything. Interestingly, is included in the faulty header file (as shown in the code copied into the question), but the problem persists anyway when std::u16string is referenced (also shown in the code). I'm not sure what the real cause is as I'm struggling with the same issue myself. – Alex Jansen Oct 22 '15 at 20:25
-1

The problem is in the file ExpGame.pro.I delete code as bellow:

QMAKE_CXXFLAGS += -std=c++11

And it is OK.

shengfu zou
  • 560
  • 1
  • 7
  • 16
  • This removes support for C++11, so you won't be able to use any C++11 features. I don't feel that this is a good final solution. – Alex Jansen Oct 22 '15 at 20:27