我用VC++2008写的用FORMAT 格式化字符串怎么不对呢?

来源:百度知道 编辑:UC知道 时间:2024/05/30 20:58:46
我建立了以个简单的MFC框架,然后在
void CMy1View::OnDraw(CDC* pDC)
函数中插入了如下的代码
CString strOutput;
strOutput.Format("%s","Hello World!");
pDC -> TextOut(0,0,strOutput);

为什么编译的时候出现了
错误 1 error C2664: “void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char [3]”转换为“const wchar_t *”
这个错误??
很不理解,书上也是这么写的。。。我初学,真不知道怎么办了。

如果就加
pDC -> TextOut(0,0,"Hello World!");
也出现错误
错误 1 error C2664: “BOOL CDC::TextOutW(int,int,const CString &)”: 不能将参数 3 从“const char [13]”转换为“const CString &”
为什么呢?

你在头文件里包含#include <tchar.h>
然后这样初始化
strOutput.Format(_T("%s"),_T("Hello World!"));
因为默认情况下,字符串都是unicode的形式,
用unicode在nt以上的系统是一个很好的方式

CString strOutput;
strOutput.Format("%s","Hello World!");
pDC -> TextOut(0,0,strOutput);
修改为
CString strOutput;
strOutput.Format("%s",L"Hello World!");
pDC -> TextOut(0,0,strOutput);

因为VS里用的是Unicode编码,所以字符串要改为宽字符。在字符串前加个L

信春哥说的对,08默认是宽字符
写成strOutput.Format(_T("%s"),_T("Hello World!"));