Search code examples
c++qt4

Conversion of std::wstring to QString throws linker error


Hi I need to convert a std::wstring to QString and I tried the most obvious way of

std::wstring wideString;
QString qtString = QString::fromStdWString(wideString);

I got the error:

Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromStdWString(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > const &)" (__imp_?fromStdWString@QString@@SA?AV1@ABV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@@Z)

referenced in function "public: void __thiscall FileHandler::deleteDir(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >&,int,unsigned int,bool,bool)" (?deleteDir@FileHandler@@QAEXAAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@HI_N1@Z) filehandler.obj

Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\Documents\Visual Studio 2008\Projects\product_dev\deletefiles\Debug\FileHandler.exe

I also tried using the method QString::fromWCharArray:

qstring temp = QString::fromWCharArray(const_cast<wchar_t*>(wideString.c_str()),wideString.size());

The error I get is

Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QString __cdecl QString::fromWCharArray(wchar_t const*,int)" (__imp_?fromWCharArray@QString@@SA?AV1@PB_WH@Z)

referenced in function "public: void __thiscall FileHandler::deleteDir(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >&,int,unsigned int,bool,bool)" (?deleteDir@FileHandler@@QAEXAAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@HI_N1@Z) filehandler.obj

Error 3 fatal error LNK1120: 1 unresolved externals C:\Users\Documents\Visual Studio 2008\Projects\product_dev\deletefiles\Debug\FileHandler.exe 1

How do I proceed to resolve this issue?


Solution

  • Edit your Visual Studio project settings and under C/C++ -> Language set the option Treat wchar_t was Built-in Type to No.