求助,LINK : error LNK2001: 无法解析的外部符号 ,代码应该没错啊,全部照书抄的啊····求助!!!!!!!!!!!!!!!!
刚开始学习windows编程,第一个程序就出错,以下是代码:照书抄的,
#include<windows.h>
CHAR szTitle[]="Windows 编程技术01章01例";
CHAR szWindowsClass[]=""EX0101";
BOOL MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE,int);
LRESULT CALLBACK WndProc(HWND,UNIT,WPARAM,LPARAM);
void MyPrint(HWND hWhd);
int CALLBACK WinMain
(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if(!InitInstance(hInstance,nCmdShow))
{
return FALSE;
}
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
BOOL MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc=(WNDPROC)WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=NULL;
wcex.hCursor=NULL;
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName=NULL;
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=NULL;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
HWND hWnd=CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,400,250,NULL,NULL,hInstance,NULL);
if(! hWnd)
{
return FALSE;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd,UNIT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
MyPrint(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void MyPrint(HWND hWnd)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps);
char szText[]="手工编写的Windows程序!";
::TextOut(hdc,100,100,szText,lstrlen(szText));
EndPaint(hWnd,&ps);
}
[解决办法]
不是代码问题,
设置问题,
要么就是工程建错,
要么就是没有引入 相关 lib
把错误提示全部 贴出来!
[解决办法]
//总体看就是一些拼写错误,所以只管照着我这个复制就OK。
#include<windows.h>CHAR szTitle[]="Windows 编程技术01章01例";CHAR szWindowsClass[]="EX0101";BOOL MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE,int);LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);void MyPrint(HWND hWhd);int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){ MSG msg; MyRegisterClass(hInstance); if(!InitInstance(hInstance,nCmdShow)) { return FALSE; } while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;}BOOL MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; wcex.cbSize=sizeof(WNDCLASSEX); wcex.style=CS_HREDRAW|CS_VREDRAW; wcex.lpfnWndProc=(WNDPROC)WndProc; wcex.cbClsExtra=0; wcex.cbWndExtra=0; wcex.hInstance=hInstance; wcex.hIcon=NULL; wcex.hCursor=NULL; wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName=NULL; wcex.lpszClassName=szWindowsClass; wcex.hIconSm=NULL; return RegisterClassEx(&wcex);}BOOL InitInstance(HINSTANCE hInstance,int nCmdShow){ HWND hWnd=CreateWindow(szWindowsClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,400,250,NULL,NULL,hInstance,NULL); if(! hWnd) { return FALSE; } ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE;}LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){ switch(message) { case WM_PAINT: MyPrint(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0;}void MyPrint(HWND hWnd){ PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); char szText[]="手工编写的Windows程序!"; ::TextOut(hdc,100,100,szText,lstrlen(szText)); EndPaint(hWnd,&ps);}
[解决办法]
如果非要说哪里错了的话,给你举两个你这个程序的拼写错误例子看吧
1)::符号你用成了中文的::
2)szWindowsClass你少了个s 比如你抄写成了szWindowClass这样显然是非定义的。
[解决办法]
1 CHAR szWindowsClass[]="EX0101";
2 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
3 wcex.lpszClassName=szWindowsClass;
4......
调试方法 双击错误信息定位到行 自己找错误信息 发现你好多都拼错了
[解决办法]
确实是有很多拼写错误。拼写错误的地方4楼已经说了
以下是我改正的。能运行
#include<windows.h>CHAR szTitle[]="Windows 编程技术01章01例";CHAR szWindowsClass[]="EX0101";BOOL MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE,int);LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);void MyPrint(HWND hWhd);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){ MSG msg; MyRegisterClass(hInstance); if(!InitInstance(hInstance,nCmdShow)) { return FALSE; } while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;}BOOL MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; wcex.cbSize=sizeof(WNDCLASSEX); wcex.style=CS_HREDRAW|CS_VREDRAW; wcex.lpfnWndProc=(WNDPROC)WndProc; wcex.cbClsExtra=0; wcex.cbWndExtra=0; wcex.hInstance=hInstance; wcex.hIcon=NULL; wcex.hCursor=NULL; wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName=NULL; wcex.lpszClassName= szWindowsClass;//szWindowClass wcex.hIconSm=NULL; return RegisterClassEx(&wcex);}BOOL InitInstance(HINSTANCE hInstance,int nCmdShow){ HWND hWnd=CreateWindow(szWindowsClass,szTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,400,250,NULL,NULL,hInstance,NULL); if(! hWnd) { return FALSE; } ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE;}LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){ switch(message) { case WM_PAINT: MyPrint(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0;}void MyPrint(HWND hWnd){ PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); char szText[]="手工编写的Windows程序!"; ::TextOut(hdc,100,100,szText,lstrlen(szText)); EndPaint(hWnd,&ps);}
[解决办法]
含这个_WinMainCRTStartup的库没被包含到你的项目里
[解决办法]
A里面有10处以上文字或标点错误
B里面没有文字或标点错误并敢为此跟人打赌
C里面没有文字或标点错误并且字体和排版完全与原稿一致
D打印在半透明的纸上和原稿重叠在一起检查一模一样,且自我感觉很有成就感
A不适合编程(理由:打字准确度偏低、粗心大意)
B初级程序员(理由:打字准确度很高、认真细致、自信、理解全角半角概念)
C高级程序员(理由:在B的基础上理解字体和排版也是电脑打印的重要因素、但相比D还不够偏执、精益求精、结果可验证)
D软件项目经理(理由:能针对项目给出令人信服的细致到极点的需求说明和典型测试用例。用户几乎挑不出毛病。专业!)
如果想从A变成B的话,到我的资源http://download.csdn.net/detail/zhao4zhong1/4084259里面下载“适合程序员的键盘练习”
[解决办法]
LZ应该是工程建错了
[解决办法]
工程创建错了。。
估计楼主建立了控制台应用程序、、、
建议win32 工程、。。楼主
[解决办法]
看起来应该把WIN32当成 console
在VC6.0中比较无敌的一句 不知道在VC10.0还行不
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"WinMainCRTStartup\"" )
放到#include<windows>下一行试下
[解决办法]
#include<windows.h>// 全局变量:HINSTANCE hInst; // 当前实例WCHAR szTitle[25]=L"Windows 编程技术01章01例"; // 这里改为宽字符WCHAR 还有字符串前面加上LWCHAR szWindowClass[7]=L"EX0101"; // 这里改为宽字符WCHAR 还有字符串前面加上Lvoid MyPrint(HWND hWhd);// 此代码模块中包含的函数的前向声明:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ MSG msg; MyRegisterClass(hInstance); if(!InitInstance(hInstance,nCmdShow)) { return FALSE; } while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int) msg.wParam;}ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wcex; wcex.cbSize=sizeof(WNDCLASSEX); wcex.style=CS_HREDRAW|CS_VREDRAW; wcex.lpfnWndProc=(WNDPROC)WndProc; wcex.cbClsExtra=0; wcex.cbWndExtra=0; wcex.hInstance=hInstance; wcex.hIcon=NULL; wcex.hCursor=NULL; wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName=NULL; wcex.lpszClassName=szWindowClass; wcex.hIconSm=NULL; return RegisterClassEx(&wcex);}BOOL InitInstance(HINSTANCE hInstance,int nCmdShow){ HWND hWnd=CreateWindow(szWindowClass,reinterpret_cast<LPCWSTR>(szTitle),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,400,250,NULL,NULL,hInstance,NULL);//这里有个强制转换需要注意 if(! hWnd) { return FALSE; } ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); return TRUE;}LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){ switch(message) { case WM_PAINT: MyPrint(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,message,wParam,lParam); } return 0;}void MyPrint(HWND hWnd){ PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); WCHAR szText[]=L"手工编写的Windows程序!";// 这里改为宽字符WCHAR 还有字符串前面加上L ::TextOut(hdc,100,100,reinterpret_cast<LPCWSTR>(szText),lstrlen(reinterpret_cast<LPCWSTR>(szText)));//这里有两个个强制转换需要注意 EndPaint(hWnd,&ps);}
[解决办法]
//这是成功后的截图
[解决办法]
楼主你用的编译器和我发的图显示的编译器一样吗? 我用的是VS2010中文旗舰版。C++的代码一般都是新编译器可以运行旧代码。《windows程序设计》这本书属于畅销书的,不过最新版用的还是旧编译器,这本书很久都没出新版了。《windows核心编程》用的是VS2005,这本书是《windows程序设计》的深入,用得也不是VS2010,能找到的经典windows书籍就这两本。感觉编译器更新了,但是书没更新。所以有的时候还是要靠自己研究。直接COPY的方法对于学windows编程不太适用了,我就是研究了好几个小时才把windows书中VC++6.0的代码转化成VS2010的。
[解决办法]
拼写错误
新建工程时选择win32 console程序,用main函数。
win32 application用WinMain