程序员都应该熟练掌握最基本、最简单的“极小化程序调试法”

程序经常会出这个问题或者那个问题, 遇到问题, 不要总是第一时间问别人, 要多学会自己去调试。 在本文中, 我来介绍“极小化程序调试法”, 这个名字是我自己取的, 其实呢, 它类似于我们经常说的的二分查找。

我们先来看一段程序(环境是VC++6.0), 这段程序是我某次在家玩代码的时候写的:

#include <windows.h>#include <iostream>using namespace std;// 对象的id值typedef enum{ErrorId = -1,IntegerId = 1,PointId = 2,RectangeId = 3,}ObjectID;// 基类struct Basic{ObjectID id;virtual Basic *copy() = 0; // 纯虚函数};// 整数类struct Integer : public Basic{int a;Basic *copy(){Integer *p = new Integer;p->a = ((Integer*)this)->a;p->id = ((Integer*)this)->id;return p;}};// 点类struct Point : public Basic{int x;int y;Basic *copy(){Point *p = new Point;p->x = ((Point*)this)->x;p->y = ((Point*)this)->y;p->id = ((Point*)this)->id;return p;}};// 矩形类struct Rectangle : public Basic{Point point;int width;int height;Basic *copy(){Rectangle *p = new Rectangle;p->point.x = ((Rectangle*)this)->point.x;p->point.y = ((Rectangle*)this)->point.y;p->width = ((Rectangle*)this)->width;p->height = ((Rectangle*)this)->height;p->id = ((Rectangle*)this)->id;return p;}};// 抽象对象的共同点, 构造成新的结点, 便于链接typedef struct node{node *next;Basic *pBasic;}Node;Node *head = NULL; // 指向第一结点(采用不带头结点的链表)// 往链式消息队列中塞消息Node *addToMsgQueue(Basic* pb){Node *pn = new Node;Node *qn = NULL;Basic *p = pb->copy(); // 多态性if(NULL == head){head = pn;}else{qn = head;while(NULL != qn->next){qn = qn->next;}qn->next = pn;}pn->pBasic = p; // 千万别忘记啊pn->next = NULL; // 千万别忘记啊return head;}// 从链式消息队列中取出消息(结点)Node *getMsgFromQueue(){if(NULL == head){return NULL;}Node *pn = head;head = head->next;return pn;} // 线程函数DWORD WINAPI ThreadFun(LPVOID pM){Node *p = NULL;// 从消息队列中取出消息while(1){p = getMsgFromQueue();if(NULL == p){Sleep(100);continue;}// 对指针进行还原switch(p->pBasic->id) {case IntegerId:{cout << ((Integer*)(p->pBasic))->a << endl;break;}case PointId:{cout << ((Point *)(p->pBasic))->x << endl;cout << ((Point *)(p->pBasic))->y << endl;break;}case RectangeId:{cout << ((Rectangle *)(p->pBasic))->point.x << endl;cout << ((Rectangle *)(p->pBasic))->point.y << endl;cout << ((Rectangle *)(p->pBasic))->width << endl;cout << ((Rectangle *)(p->pBasic))->height << endl;break;}default:{break;}}}return 0;}// 主线程int main(){HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL);CloseHandle(handle);// 定义三个对象并赋值Integer i;Point po;Rectangle rect;i.id = IntegerId;po.id = PointId;rect.id = RectangeId;i.a = 11;po.x = 22;po.y = 33;rect.point.x = 44;rect.point.y = 55;rect.width = 66;rect.height = 77;// 塞入消息队列while(1){addToMsgQueue(&i);addToMsgQueue(&po);addToMsgQueue(&rect);Sleep(2000);}return 0;} 我估计, 如果只凭肉眼, 应该没有人能看出上面程序有什么编译错误, 如果能看出来, 要么是个天才, 要么是个废材。

好吧, 我们来编译一下:发现错误如下:

——————–Configuration: test – Win32 Debug——————–Compiling…main.cppC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(162) : error C2059: syntax error : ‘)’C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(163) : error C2059: syntax error : ‘)’C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(164) : error C2059: syntax error : ‘)’C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(165) : error C2059: syntax error : ‘)’C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : error C2146: syntax error : missing ‘;’ before identifier ‘rect’C:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : warning C4551: function call missing argument listC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(188) : error C2065: ‘rect’ : undeclared identifierC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(192) : error C2228: left of ‘.id’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(198) : error C2228: left of ‘.point’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(198) : error C2228: left of ‘.x’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(199) : error C2228: left of ‘.point’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(199) : error C2228: left of ‘.y’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(200) : error C2228: left of ‘.width’ must have class/struct/union typeC:\Documents and Settings\Administrator\桌面\cpp\test\main.cpp(201) : error C2228: left of ‘.height’ must have class/struct/union typeError executing cl.exe.main.obj – 13 error(s), 1 warning(s)

烦恼忧愁不用追,吃点好的别嫌贵,

程序员都应该熟练掌握最基本、最简单的“极小化程序调试法”

相关文章:

你感兴趣的文章:

标签云: