對專案進行偵錯 (C++)
更新:2007 年 11 月
在這個步驟中,您會修改程式,以修正在測試專案時所發現的問題。
必要條件
本主題假設您已了解 C++ 語言的基礎。如果您才剛開始學習 C++,建議您參考 Herb Schildt 所著的《C++ Beginner's Guide》。您可以在 https://go.microsoft.com/fwlink/?LinkId=115303 找到這份文件的線上版本。
修正有錯誤的程式
若要查看 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 建構函式 (Constructor) 執行後,totalparticipants 的值都會增加。在每個指標刪除 (而且呼叫解構函式 (Destructor)) 之後,totalparticipants 都會減少。
逐步執行至程式的最後一行。在即將執行 return 陳述式之前,totalparticipants 會等於 0。請繼續逐步執行程式到結束為止,或是在 [偵錯] 功能表上,按一下 [執行],或按 F5 鍵繼續執行到結束為止。
後續步驟
上一個主題:測試專案 (C++) | 下一個主題:部署程式 (C++)