閱讀394 返回首頁    go 阿裏雲 go 技術社區[雲棲]


[error] MFC錯誤不能將參數1從"const char [3]"轉換為"const wchar_t *"

在做項目中經常遇到很多錯誤,這裏我僅僅把自己遇到的一些錯誤和解決方法寫出,供自己和大家查看.
代碼如下:

CRect rect;
GetClientRect(&rect);
CString str;
str.Format("%ld",rect.bottom);
MessageBox(str);

錯誤提示:
error C2664: “void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能將參數 1 從“const char [3]”轉換為“const wchar_t *”.
解決方案:
1.由於VS默認字符集為UNICODE,可以使用_T("")宏,讓程序支持Unicode編碼.定義於tchar.h.(詳見百科:_T())

/* Generic text macros to be used with string literals and character constants.
   Will also allow symbolic constants that resolve to same. */

#define _T(x)       __T(x)
#define _TEXT(x)    __T(x)

使用時的具體代碼方法:str.Format(_T("%ld"),rect.bottom);
注意我在使用:str.Format(_T("%ld %ld %d %d",rect.left,rect.top,rect.Height(),rect.Width()));會提示warning:_T()實參太多,而且對應的數據也是錯誤的.故應修改成str.Format(_T("%ld %ld %d %d"),rect.left,rect.top,rect.Height(),rect.Width());正確.

2.同時也可以把工程設置多字節集.
設置步驟:調試->XXX項目 屬性->配置屬性->常規->字符集->使用多字節集.但建議使用Unicode能避免很多字節型錯誤.

雖然這類文章在百度很多,但我還是想以自己的視角寫這類一些列自己遇到的錯誤的解決方法.最後希望該文章對大家有所幫助.參考資料:https://bbs.csdn.net/topics/380162797
(原創By:Eastmount 2014-2-26 下午5點https://blog.csdn.net/eastmount/)
 

最後更新:2017-04-03 12:55:16

  上一篇:go FAILED BINDER TRANSACTION
  下一篇:go CalledFromWrongThreadException