请教一个c++问题,高手来

来源:百度知道 编辑:UC知道 时间:2024/05/30 13:44:39
#include <afxwin.h>

class MyWindows:public CFrameWnd
{
public:
MyWindows()
{
Create(NULL,"My Application Windows");
MessageBox("Windows Created","CFrameWnd Constructor");
}
};

class MyApp:public CWinApp
{
public:
bool InitInstance()
{
MyWindows *MyWindowsObject = new MyWindows();
m_pMainWnd= MyWindowsObject;
MyWindowsObject->ShowWindow(SW_SHOWNORMAL);
return true;
}
};

MyApp MyAppObject;

这行代码有错么?

为何vs08报错
错误 1 error C2664: “CFrameWnd::Create”: 不能将参数 2 从“int”转换为“LPCTSTR” f:\疯狗残云\documents\visual studio 2008\projects\sample\sample\sample.cpp 8 sample
错误 2 error C2664: “CWnd::MessageBoxW”: 不能将参数 1 从“const char [16]”转换为“LPCTSTR” f:\疯狗残云\documents\visual studio 2008\projects\sample\sample\sample.cpp 9 sample
错误 3 error C25

我在vs2005上调试通过了,修改了两个地方,不多说了,代码如下,自己参详吧:
#include <afxwin.h>

class MyWindows:public CFrameWnd
{
public:
MyWindows()
{
Create(NULL,L"My Application Windows");
MessageBox(L"Windows Created",L"CFrameWnd Constructor");
}
};

class MyApp:public CWinApp
{
public:
BOOL InitInstance()
{
MyWindows *MyWindowsObject = new MyWindows();
m_pMainWnd= MyWindowsObject;
MyWindowsObject->ShowWindow(SW_SHOWNORMAL);
return true;
}
};
注意,还没有main()函数的啊,你自己工程应该有吧~

字符类型的问题!
VS2008默认使用UNICODE编码的,一个字符是两个字节!上面的代码应该修改成这样:
MyWindows()
{
Create(NULL,L"My Application Windows");
MessageBox(L"Windows Created",L"CFrameWnd Constructor");
}
即系在字符串前面加上一个L,L是一个宏!
Create函数需要LPCTSTR类型的参数, LPCTSTR是一个宏,如果定义了UNICODE,则LPCTSTR解释为LPCWSTR,其中的W就是Wide的意思,即系宽,也就是上面说的
一个字符