Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
470 views
in Technique[技术] by (71.8m points)

c++ - 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:UsersDocumentsVisual Studio 2008Projectsproduct_devdeletefilesDebugFileHandler.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:UsersDocumentsVisual Studio 2008Projectsproduct_devdeletefilesDebugFileHandler.exe 1

How do I proceed to resolve this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Best solution to the problem is to set the option "Treat wchar_t was Built-in Type" to No. However in some cases this might not be possible.

For example xerces_c is compiled with wchar_t as Built-in Type. If you need to use both xerces_c then you must either recompile QT or xerces_c to match a common Built-in Type setting.

Windows uses UTF16 charset so does QT for unicode strings. Thus, the alternative solution below might be a life saver.

/*! Convert a QString to an std::wstring */
std::wstring qToStdWString(const QString &str)
{
#ifdef _MSC_VER
    return std::wstring((const wchar_t *)str.utf16());
#else
    return str.toStdWString();
#endif
}

/*! Convert an std::wstring to a QString */
QString stdWToQString(const std::wstring &str)
{
#ifdef _MSC_VER
    return QString::fromUtf16((const ushort *)str.c_str());
#else
    return QString::fromStdWString(str);
#endif
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...