调试项目 (C++)
更新:2007 年 11 月
在本步骤中,您将修改程序以修复在测试项目时发现的问题。
先决条件
本主题假定您具备 C++ 语言的基础知识。如果您是刚开始学习 C++,建议您参阅 Herb Schildt 编写的 C++ Beginner's Guide(《C++ 初学者指南》),该书可从 https://go.microsoft.com/fwlink/?LinkId=115303 在线获得。
修复包含 bug 的程序
若要明白在 Cardgame 对象销毁时会发生什么,请查看 Cardgame 类的析构函数。
在“视图”菜单上,单击“类视图”,或者单击“解决方案资源管理器”窗口中的“类视图”选项卡。
展开“game”项目树并单击“Cardgame”类。
下方的区域显示类的成员和方法。
右击“~Cardgame(void)”析构函数并单击“转到定义”。
要在 Cardgame 终止时减少 totalparticipants ,请在 Cardgame::~Cardgame 析构函数的左大括号和右大括号之间键入以下代码:
totalparticipants -= players; cout << players << " players have finished their game. There are now " << totalparticipants << " players in total." << endl; }
进行上述更改后,Cardgame.cpp 文件应如下所示:
#include "Cardgame.h" #include <iostream> using namespace std; Cardgame::Cardgame(int p) { players = p; totalparticipants += p; cout << p << " players have started a new game. There are now " << totalparticipants << " players in total." << endl; } Cardgame::~Cardgame(void) { totalparticipants -= players; cout << players << " players have finished their game. There are now " << totalparticipants << " players in total." << endl; }
在“生成”菜单上,单击“生成解决方案”。
在“调试”菜单上,单击“运行”,或按 F5,以“调试”模式运行该程序。程序将在第一个断点处暂停。
在“调试”菜单上单击“逐过程”或者按 F10 逐句通过程序。
请注意,执行每个 Cardgame 构造函数后,totalparticipants 的值会增大。而在删除每个指针(并调用析构函数)后,totalparticipants 的值会减小。
单步执行至程序的最后一行。恰好在执行 return 语句之前,totalparticipants 等于 0。继续逐句通过程序,直到程序退出;或者,在“调试”菜单上单击“运行”或按 F5,允许程序继续运行,直到退出。
后续步骤
上一部分:测试项目 (C++) | 下一部分:部署程序 (C++)