请教一个LNK2001: unresolved external symbol "int __cdecl Run(void)"的错误!
我写了一个程序:
// tzy2.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "windows.h"
HINSTANCE hInst;
HWND hWnd;
MSG msg;
char lpszClassName[]="窗口";
char *ShowText;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL Create(HINSTANCE,int);
int Run();
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MyRegisterClass(hInstance);
Create(hInstance,nCmdShow);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return Run();
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASS wc;
wc.style=0;
wc.lpfnWndProc=WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=hInstance;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName=NULL;
wc.lpszClassName=lpszClassName;
return RegisterClass(&wc);
}
BOOL Create(HINSTANCE hInstance,int nCmdShow)
{
hWnd=CreateWindow(lpszClassName,"windows",WS_OVERLAPPEDWINDOW,400,300,180,160,NULL,NULL,hInstance,NULL);
return TRUE;
}
int RUN()
{
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
OnPaint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
OnDestroy(hWnd,message,wParam,lParam);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
void OnLButtonDown(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
ShowText="Hello!";
InvalidateRect(hWnd,NULL,1);
}
void OnPaint(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,50,50,ShowText,6);
EndPaint(hWnd,&ps);
}
void OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
}
在编译的时候没有报错,也没有警告。
但是在运行的时候,报:
--------------------Configuration: tzy2 - Win32 Debug--------------------
Linking...
tzy2.obj : error LNK2001: unresolved external symbol "int __cdecl Run(void)" (?Run@@YAHXZ)
Debug/tzy2.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
tzy2.exe - 1 error(s), 0 warning(s)
请那位高手指点一下,怎么回事?
[解决办法]
你声明时用int Run();
实现时用int RUN()
{
while(GetMessage(&msg,NULL,0,0))
。。。。。。
C++是区分大小的,Run和RUN不一样,将实现中的RUN改成Run就行了。