将visual studio的std :: wstring移植到mingw gcc [英] porting std::wstring from visual studio to mingw gcc
问题描述
我正在将一些代码从Visual Studio移植到mingw gcc。
我碰到过这个声明
if(mnode.GetTag()== _T(val))
返回true;
这是GetTag()方法的定义
const std :: wstring& GetTag()const;
我收到错误
错误:没有匹配函数调用'std :: basic_string< wchar_t> :: basic_string(const char [6])'
现在阅读这个我仍然不确定如何解决这个问题。
有关为何显示此错误的任何建议?是因为wstring?
看起来像你的问题是 _UNICODE
预处理器宏没有被定义。 MSDN解释这将如何影响包含在 _T()
macro。
pWnd-> SetWindowText(_T(Hello ));
定义了
_UNICODE
,_T
将文字字符串转换为L
- 前缀形式;否则,_T
将转换字符串而不是L
前缀。
在字符串文字中添加 L
前缀表示它是 std :: basic_string< wchar_t>),以及 std :: wstring )定义了一个
运算符==
重载,它需要一个 wchar_t const *
参数,从而允许您的代码进行编译。
请注意,如果您打算使用
因此,解决问题的一种方法是添加 _UNICODE
和 UNICODE
预处理器符号到gcc命令行。
但是不要这样做!这是我对此事的看法—而不是依赖于模糊的宏,只需手动在 L
之前加上字符串前缀即可。特别是在这种情况下,既然你说 GetTag()
总是返回一个 wstring const&
,我会说使用
甚至在调用Windows API函数时,只是明确地调用宽字符版本。例如,用 GetWindowTextW
替换对 GetWindowText
的调用。
I am in the process of porting some code from Visual studio to mingw gcc. I came across this statement
if ( mnode.GetTag() == _T( "val" ) )
return true;
this is the definition of GetTag() method
const std::wstring &GetTag() const;
I am getting the error
error: no matching function for call to 'std::basic_string<wchar_t>::basic_string(const char [6])'|
Now after reading this I am still not sure how to resolve this issue. Any suggestions on why this error is being shown up ? is it because of wstring ?
It looks like your problem is that the _UNICODE
preprocessor macro is not defined. MSDN explains how this affects string literal enclosed in the _T()
macro.
pWnd->SetWindowText( _T("Hello") );
With
_UNICODE
defined,_T
translates the literal string to theL
-prefixed form; otherwise,_T
translates the string without theL
prefix.
Adding an L
prefix to a string literal indicates it is a wide string literal, and std::wstring
(or std::basic_string<wchar_t>
) defines an operator==
overload that takes a wchar_t const *
argument, thus allowing your code to compile.
Be aware that there's also a UNICODE
macro that's relevant if you're going to be calling functions in the Windows API. Raymond Chen explains the madness quite well in this post.
So, one way to fix your problem is to add both _UNICODE
and UNICODE
preprocessor symbols to the gcc command line.
But don't do this! This is my opinion on the matter — instead of depending on obscure macros just prefix the string literal with L
manually. Especially in this case, since you say that GetTag()
always returns a wstring const&
, I'd say using the _T()
macro for that string literal is a bug.
Even otherwise, when calling Windows API functions, just call the wide character version explicitly. For instance, replace calls to GetWindowText
with GetWindowTextW
.
这篇关于将visual studio的std :: wstring移植到mingw gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!