Share via


연습: 프로젝트 디버깅(C++)

이 연습에서는 프로젝트를 테스트할 때 발견한 문제를 해결하려면 프로그램을 수정합니다.

사전 요구 사항

버그가 있는 프로그램을 수정하려면

  1. Cardgame 개체가 제거될 때 발생하는 상황을 보려면 Cardgame 클래스에 대한 소멸자를 봅니다.

    메뉴 모음에서 보기>클래스 뷰를 선택합니다.

    클래스 뷰 창에서 게임 프로젝트 트리를 확장하고 Cardgame 클래스를 선택하여 클래스 멤버와 메서드를 표시합니다.

    ~Cardgame(void) 에 대한 바로 가기 메뉴를 연 다음, 정의로 이동을 선택합니다.

  2. Cardgame이 종료될 때 totalParticipants을 감소하려면 Cardgame::~Cardgame 소멸자의 여는 중괄호와 닫는 중괄호 사이에 다음 코드를 추가합니다.

    totalParticipants -= players;
    cout << players << " players have finished their game.  There are now "
         << totalParticipants << " players in total." << endl;
    
  3. 변경한 후 Cardgame.cpp 파일은 아래 코드와 유사합니다.

    #include "Cardgame.h"
    #include <iostream>
    
    using namespace std;
    
    int Cardgame::totalParticipants = 0;
    
    Cardgame::Cardgame(int players)
        : players(players)
    {
        totalParticipants += players;
        cout << players << " players have started a new game.  There are now "
             << totalParticipants << " players in total." << endl;
    }
    
    Cardgame::~Cardgame()
    {
        totalParticipants -= players;
        cout << players << " players have finished their game.  There are now "
             << totalParticipants << " players in total." << endl;
    }
    
  4. 메뉴 모음에서 빌드>솔루션 빌드를 선택합니다.

  5. 빌드가 완료되면 메뉴 모음에서 디버그>디버깅 시작을 선택하거나 F5 키를 선택하여 디버그 모드에서 실행합니다. 프로그램은 첫 번째 중단점에서 일시 중지됩니다.

  6. 프로그램을 단계별로 실행하려면 메뉴 모음에서 디버그>프로시저 단위 실행을 선택하거나 F10 키를 선택합니다.

    Cardgame 생성자가 실행된 후 totalParticipants의 값이 증가합니다. 각 Cardgame 인스턴스가 범위를 벗어나고 삭제될 때(소멸자가 호출될 때) PlayGames 기능이 반환되는 경우 totalParticipants가 감소합니다. return 문이 실행되기 바로 전에 totalParticipants는 0과 같습니다.

  7. 메뉴 모음에서 디버그>실행을 선택하거나 F5 키를 선택하여 종료될 때까지 프로그램을 단계별로 실행을 계속하거나 실행되게 합니다.

다음 단계

이전:연습: 프로젝트 테스트(C++)
다음:연습: 프로그램 배포(C++)

참조

C++ 언어 참조
프로젝트 및 빌드 시스템