Hello, friends! I have this question: there is a class in it, I defined a vector with type string. And I call this class with this array and I want to transfer the name of the file there, but it displays an error stating that the array has gone out of the border (vector substript out of range). How can this be avoided? Thank you in advance. Here is the class:

class resqmobject { public: string title; string uid; string description; vector <float> floatdata; vector <int> intdata; vector <SplitReferenses> SRData; vector <FaultStruct> FData; vector <coord> CoordData; vector <float> ZCORN; vector <string> FileName; }; 

And this is how I want to add the file name to the vector:

 for (int i = 0; cursor < arr.size(); i++){ string strFileName = ofn.F_Pillars; ofstream fout(ofn.F_Pillars); char *c = new char[strFileName.length() + 1]; FPObject.FileName[i].push_back(*c); } 
  • Once again and slowly ... FPObject.FileName is a vector of lines, FPObject.FileName[i] is the i-th line in it, FPObject.FileName[i].push_back(*c); - an attempt to add the first character from the (uninitialized) array c to the i-th line of your vector. It may add (although what do you get?), But you, judging by the error message, have many more elements in arr than in FileName . - Harry
  • Thanks for the replies friends. So far, figured out)) - Shokhan

2 answers 2

Brr ...

 string strFileName = ofn.F_Pillars; ... // Зачем вы тут открываете файл - не знаю, это ваши дела... FPObject.FileName.push_back(strFileName); 

And what you do in the cycle, I also do not understand (what arr - you have no description). The main thing is to add a line to the vector of lines - call push_back() for this vector, passing it a line.

    not added, displays an error Expression: vector subscript out of range. How can I avoid this and add an element to an array?