delete的错误
写的是一个记事本程序,可是我按Backspace键时。运行出错,
[code=C/C++]
if(wParam==VK_BACK)
{
if(head-> next==NULL)
MessageBox(hwnd, "最前端 ",NULL,MB_OK);
else
{
if(curp-> next == NULL)
{
prep = prep-> pre ;
delete curp;
curp = prep-> next ;
curp-> next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}
else //当光标不在最后的位置时,删除当前节点,移动的光标功能我还没写,所以这里可以不要看
{
prep-> next = curp-> next ;
curp-> next -> pre = prep;
delete curp;
curp = prep-> next ;
}
InvalidateRect(hwnd,NULL,TRUE);
}
break;
}
[/code]
这是那一段代码,我也debug了,明明没看出delete出错了,
你们看下我节点的定义 和上面粘贴的代码就可以了, 我也把完整的代码贴上来,
[code=C/C++]
/*3月10日,全部刷新显示*/
#include <windows.h>
#include <stdlib.h>
#include <string.h>
struct LNode
{
UINT data;
struct LNode* next;
struct LNode* pre;
};
LNode* InitClist()
{
LNode* head = new LNode;
head-> next = NULL;
head-> pre = NULL;
head-> data=0;
return head;
}
LNode* curp; //当前节点指针
LNode* prep; //前一节点指针
LNode* head; //头节点指针
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
char lpszClassName[]= "Window ";
char lpszTitle[]= "NotePad ";
wndclass.style = CS_OWNDC;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszClassName ;
if(!RegisterClass(&wndclass))
return FALSE;
hwnd= CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,
hInstance,
NULL);
head=InitClist();
curp=head;
prep=NULL;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam ,LPARAM lParam)
{
intxchar=0,ychar=0; //存放当前串所占的宽度和高度
int num;
LNode* print;
static BOOL Word= TRUE;
RECT rtClient;
HDC hdc;
HCURSOR hCursor;
TEXTMETRIC tm;
PAINTSTRUCT ps;
SIZE size;
static char buf[2];
static int nNumChar=0;
static int nArrayPos=0;
switch(message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
GetTextMetrics(hdc,&tm);
ReleaseDC(hwnd,hdc);
CreateCaret(hwnd,NULL,2,tm.tmHeight-2 ); //创建一个标记,比字符高度小2个逻辑单位
ShowCaret(hwnd); //显示标记
break;
case WM_PAINT: //应增加 改变窗口大小就全部重绘的代码
hdc=BeginPaint(hwnd,&ps);
SetTextColor(hdc,RGB(255,0,0));
GetTextMetrics(hdc,&tm);
ychar= tm.tmHeight+tm.tmExternalLeading ;
if(head-> next != NULL)
{
print=head-> next ;
for(num=0;num <head-> data ;num++)
{
if(print-> data <128)
{
if(print-> data !=13)
{
buf[0]= char(print-> data );
GetTextExtentPoint32(hdc,&buf[0],1,&size);
xchar = xchar+size.cx;
::GetClientRect(hwnd,&rtClient);
if(xchar> =rtClient.right )
{
xchar = size.cx ;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
TextOut(hdc,xchar-size.cx,ychar-size.cy,buf,1);
}
else
{
xchar = 0;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
print=print-> next ;
}
else
{
buf[0]=print-> data /256;
buf[1]=print-> data %256;
GetTextExtentPoint32(hdc,buf,2,&size);
xchar = xchar+size.cx;
::GetClientRect(hwnd,&rtClient);
if(xchar> =rtClient.right )
{
xchar = size.cx ;
ychar = ychar+ size.cy + tm.tmExternalLeading ;
}
TextOut(hdc,xchar-size.cx,ychar-size.cy,buf,2);
print=print-> next ;
}
}
}
SetCaretPos(xchar,ychar-size.cy+2 );
EndPaint(hwnd,&ps);
break;
case WM_SIZE:
::InvalidateRect (hwnd,NULL,TRUE);
break;
case WM_CHAR:
if(wParam==VK_BACK)
{
if(head-> next==NULL)
MessageBox(hwnd, "最前端 ",NULL,MB_OK);
else
{
if(curp-> next == NULL)
{
prep = prep-> pre ;
//prep-> next-> next =NULL;
delete curp;
curp = prep-> next ;
curp-> next =NULL;
}
else
{
prep-> next = curp-> next ;
curp-> next -> pre = prep;
delete curp;
curp = prep-> next ;
}
InvalidateRect(hwnd,NULL,TRUE);
}
break;
}
if((unsigned)wParam <128)
{
prep=curp;
curp = new LNode;
curp-> pre = prep;
prep-> next = curp;
curp-> data = (unsigned)wParam;
curp-> next = NULL;
head-> data +=1;
InvalidateRect(hwnd,NULL,FALSE);
}
else
{
if(Word) //Word为1表示汉字的高位录入
{
prep=curp;
curp = new LNode;
prep-> next = curp;
curp-> pre = prep;
curp-> next = NULL;
Word=!Word;
curp-> data = (unsigned)wParam;
curp-> data=curp-> data < <8;
head-> data +=1;
}
else
{
curp-> data =curp-> data + (unsigned)wParam;
Word=!Word;
InvalidateRect(hwnd,NULL,TRUE);
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
[/code]
这个程序的功能主要是想实现记事本的功能,还有很多功能没有写,但是BackSpace出错了, 哪位高手帮忙解答一下。
[解决办法]
if(curp->next == NULL)
{
prep = prep->pre ;
delete curp; //这里已经被删除
curp = prep->next ;//这里又给他赋值
curp->next =NULL; //debug到这一步是,可以看到curp开始指向的节点 已 //经删除,并且移向了前一节点,即实现了删除,为什么运行时还会报错
}