2013-04-23 2 views
1

저는 C++을 처음 사용하며 사용자 정의 된 숫자 집합을 취하는 하위 집합 프로그램의 합계를 작성합니다.이 집합의 첫 번째 수가 전체로 간주됩니다. DDD를 사용하여이 프로그램을 디버깅하려했지만 한계를 벗어났습니다. 왜 이런 일이 일어나는지 알 수없는 것 같습니다. 모든 단서? 감사.서브 세트 합계 프로그램

terminate called after throwing an instance of 'std::out_of_range' 
what(): vector::_M_range_check 

코드 :

#include <iostream> 
#include <vector> 
#include <cassert> 
#include <iomanip> 
#include <climits> 
#include <math.h> 
#include <algorithm> 

typedef unsigned int uint; 
using namespace std; 

//////// Function declerations ////////// 
void sum_of_subsets(uint index, 
        uint weight, 
        uint total, 
        vector<bool> &include, 
        vector<uint> &w, 
        uint W); 

bool promising (uint index, 
       uint weight, 
       uint W, 
       vector<uint> w, 
       uint total); 

다음은 오류입니다.

/////////////// Main ////////////////// 
int main() 
{ 
    //string sortingCode = "-as"; 
    vector<uint> w;    // vector of weights 
    vector<bool> include;  
     uint W;      // the total 
    uint index = 0; 
    uint weight = 0;   // the current weight of subsets 
    uint total = 0;    // the superset total weight 
    while(! cin.eof()) 
    { 
    uint value; 
    if(cin >> value && ! cin.eof()) 
     w.push_back(value); 
    } 

    W = w.front(); 
    w.erase(w.begin()); 
    // instantiate the include vector to false 
    for(uint k = 0; k <= w.size(); k++) 
     include.push_back(0); 
    // calculate the superset total 
    for(uint k = 0; k <= w.size()-1; k++) 
     total += w.at(k); 
    // calculate the sum of subsets accordig to CL argument 
    sum_of_subsets(index, weight, total, include, w, W); 

    // report success 
    return 0; 
}  

.

////////// Function Bodies /////////// 
void sum_of_subsets(uint index, 
        uint weight, 
        uint total, 
        vector<bool> &include, 
        vector<uint> &w, 
        uint W) 
{  
    cout << "inside sumb_of_subsets" << endl; 
    if(promising(index, weight, W, w, total)) 
    { 
     cout << "promising is true, continue" << endl; 
     if(weight == W) 
     { 
      for(uint k = 0; k <= index; k++) 
      { 
       if(include.at(k)) 
        cout << w.at(k) << " ";; 
      } 
      cout << endl; 
     } 
     else 
     { 
      include.at(index + 1) = 1; 
      cout << "index1 = " << index << endl; 
      sum_of_subsets(index + 1, 
          weight + w.at(index + 1), 
          total - w.at(index + 1), 
          include, w, W) ; 
      include.at(index + 1) = 0; 
      cout << "index2 = " << index << endl; 
      sum_of_subsets(index + 1, 
          weight, 
          total - w.at(index + 1), 
          include, w, W); 
     } 
    } 
} 

.

bool promising (uint index, 
       uint weight, 
       uint W, 
       vector<uint> w, 
       uint total) 
{  
    cout << "inside promising" << endl; 
    cout << "W = " << W << endl; 
    cout << "weight = " << weight << endl; 
    return (weight + total >= W) 
     && ((weight == W) || (weight + w.at(index+1) <= W)); 
} 
+0

"하지만 범위를 벗어난 오류가 계속 발생합니다."- 오류 메시지를 공유하십시오. – Bill

+0

위의 질문을 편집하여 – Busch

+0

오류를 포함 시키셨습니까? – Bill

답변

1

이 오류 :

terminate called after throwing an instance of 'std::out_of_range' 
what(): vector::_M_range_check 

벡터의 디버그 버전은 예외를 던지는 것을 제안합니다, 아무것도를 잡는다 없습니다. 이 경우 스택을 풀지 않고 프로그램이 즉시 종료되므로 디버깅이 어려워집니다. 이것에

변경 :

// calculate the sum of subsets accordig to CL argument 
try 
{ 
    sum_of_subsets(index, weight, total, include, w, W); 
} 
catch (...) 
{ 
    cout << "put breakpoint here!" << endl; 
} 

는 캐치에 중단 점을 추가하고, 잘못된 코드의 어떤 부분 확인하기 위해 역 추적을 확인합니다.

+0

고마워, for 루프 인덱스 내 문제가 발생하지 않습니다. sum_of_subsets에 대한 재귀 호출과 관련이 있다고 생각하지만 디버깅하는 동안 어디서 왜 그 이유를 정확하게 찾아 낼 수 없었습니다. – Busch

+0

ddd에서 충돌 후 bt (또는 백 트레이스)를 입력하면 어떻게됩니까? – Bill

+0

@SeanHellebusch 확실히 당신이 한계를 벗어나는 것입니다. –