'std::out_of_range' error

Hi,
I'm a beginner with C++ and don't know much about it. I compiled the following codes without error, but while running I get the following error:
terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::erase

Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <string.h>
#include <string>
#include <fstream>
#include <CVector.h>
#include <CMatrix.h>

int main(int argc, char* args[]) {
  if (argc <= 3) {
    std::cout << "Usage: MoSegEvalAll_PR shotList.txt 10|50|200|all trackList.txt" << std::endl;
    return -1;
  }
  std::string binaryPath = args[0];
  binaryPath.erase(binaryPath.find_last_of('/')+1,binaryPath.length());
  std::ifstream aShotList(args[1]);
  std::ifstream aTrackList(args[3]);
  if (!aShotList.is_open()) {
    std::cerr << args[1] << " could not be opened." << std::endl;
    return -1;
  }
  if (!aTrackList.is_open()) {
    std::cerr << args[3] << " could not be opened." << std::endl;
    return -1;
  }
  int aEvaluationMode;
  if (strcmp(args[2],"all") == 0) aEvaluationMode = -1;
  else aEvaluationMode = atoi(args[2]);
  if (aEvaluationMode != -1 && aEvaluationMode != 10 && aEvaluationMode != 50 && aEvaluationMode != 200) {
    std::cerr << "Evaluation of " << args[2] << " frames is not allowed. Choose 10, 50, 200, or all frames." << std::endl;
    return -1;
  }
  char dummy[300];
  float objectThreshold = 0.9f;
  if (argc >= 5)
    objectThreshold = atof(args[4]);
  sprintf(dummy, "_Fgeq%4.2f", objectThreshold*100.0f);
  // Open output file
  std::string s = args[3];
  s.erase(s.find_last_of('.'),s.length());
  s += dummy; 
  s += "Numbers.txt";
  std::ofstream aOut(s.c_str());
  if (!aOut.is_open()) {
    std::cerr << "Could not write output file." << std::endl;
    return -1;
  }
  float aSumDensity = 0.0f;
  float aSumAvrgDensity = 0.0f;
  float aSumAvrgPrecision = 0.0f;
  float aSumAvrgRecall = 0.0f;
  float aSumAvrgPrecisionSq = 0.0f;
  float aSumAvrgRecallSq = 0.0f;
  int aSumExtracted = 0;
  int aSumVisibles = 0;
  int aSize;
  aShotList >> aSize; aShotList.getline(dummy,300);
  int aCounter = 0;
  for (int shot = 0; shot < aSize; shot++) {
    // Read parts of the definition file ---------------------------------------
    std::string aShotLine;
    aShotList >> aShotLine;
    std::ifstream aPropFile(aShotLine.c_str());
    if (!aPropFile.is_open()) {
      std::cerr << "Definition file " << aShotLine << "  not found." << std::endl;
      return -1;
    }
    aPropFile.getline(dummy,300);
    aPropFile.getline(dummy,300);
    // Number of regions
    aPropFile.getline(dummy,300);
    int aRegionNo;
    aPropFile >> aRegionNo; aPropFile.getline(dummy,300);
    // Number of frames
    for (int i = 0; i < 3*aRegionNo; i++)
      aPropFile.getline(dummy,300);
    aPropFile.getline(dummy,300);
    aPropFile.getline(dummy,300);
    aPropFile.getline(dummy,300);
    aPropFile.getline(dummy,300);
    int aTotalFrameNo;
    aPropFile >> aTotalFrameNo; aPropFile.getline(dummy,300);
    // Ignore this shot if it does not have the required number of frames
    if (aEvaluationMode > 0 && aTotalFrameNo < aEvaluationMode) {
      std::cout << "Only " << aTotalFrameNo << " frames. " << aShotLine << " ignored." << std::endl;
      continue;
    }
    // Remove empty lines in list of tracking files ----------------------------
    std::string aTrackLine;
    do {
      aTrackList >> aTrackLine;
      while (aTrackLine[0] == ' ')
        aTrackLine.erase(0,1);
    } while (aTrackLine.length() == 1);
    // Run evaluation tool -----------------------------------------------------
    std::string s = binaryPath + "MoSegEvalPR ";
    s += aShotLine + ' ' + aTrackLine;
    sprintf(dummy, " %f", objectThreshold);
    s += dummy;
    std::cout << "EXECUTE: " << s.c_str() << std::endl;
    if (system(s.c_str()) != 0) {
      std::cerr << "Error while running " << s << std::endl;
      return -1;
    }
    // Evaluate result file ----------------------------------------------------
    aCounter++;
    s = aTrackLine;
    s.erase(s.find_last_of('.'),s.length());
    std::ifstream aResult((s+"Numbers.txt").c_str());
    aResult.getline(dummy,300);
    aResult.getline(dummy,300);
    aResult.getline(dummy,300);
    // Number of evaluated frames
    int aEvaluatedFrames;
    aResult.getline(dummy,300);
    aResult >> aEvaluatedFrames; aResult.getline(dummy,300);
    if (aEvaluatedFrames != aEvaluationMode && aEvaluationMode > 0) {
      std::cerr << "The tracks listed in " << args[3] << " have been computed considering different numbers of frames." << std::endl;
      return -1;
    }
Last edited on
Please use code tags when posting code.

You need to be careful about lines like this:
s.erase(s.find_last_of('.'),s.length());
What happens if find_last_of() fails? Answer: Your program will crash with the error message you happen to be receiving. I think you need to carefully review some documentation for this function paying extra attention to the return value, especially when the function fails to find the item.

http://www.cplusplus.com/reference/string/string/find_last_of/

You should also read up on the std::string::npos. This is an unsigned type (a size_t) and it holds the highest value that can be held in that type.
Topic archived. No new replies allowed.