Monday, 15 July 2013

c++ - Getting out of range error, while the requested item should be in bound -


I am working on a protocol to use future projects on microcontrollers. I am using a library which will give me standard C ++ attributes, such as vector s

to use. When requesting an item, the vector is giving error outside the boundary thing is that the object should be in range because the size of the vector is larger than the requested element.

The problem occurs in the following code:

  // These two variables are defined (as a method parameter) // Some values ​​of the dummy value string Are s = "this_is_some_string"; // This is a string that should be divided into four splitChar = '_'; // This is the delimiter character // Define a vector to store string parts in the vector & lt; String & gt; STR; // split string int start = 0, end = 0; While ((end = s.find (splitChar, start))! = String :: npos {strs.push_back (s.sstr (start, end-start)); Start = end + 1; } Strs.push_back (s.substr (start)); // Print the vector size for the displaying serial. Print ("vector shape:"); Serial.println (strs.size ()); // Loop through vector (int i = 0; i  

This is a code part by some method that divides a string into several substrings by a delimiter character. The first two variables are defined using two method parameters, but I put them there for example work. serial.println (""); I am using Arduino libraries to print debug messages using which is running on my computer.

When the error occurs, the following output is given in the console:

  The size of the vector: 1 [0] Invalid subscript  
< P> Then the program ends with invalid subscript string comes from vector :: at (int i); The method that returns "invalid subscription" string when there is a request that the request is out of bound () is odd, that the requested element should not be bound. In fact, the size of the vector is 1, whereas the requested element is at position 0 (this should be the only element which can be requested in this case). In addition, string :: c_str (); The method is used to create four arrays to print in the console, because arduino serial.println (""); Method

Itemprop = "text">

Such It seems that :: does not work with the Arduino IDE, try to use strlen () - 1 instead of

  while ((end = s.find (splitChar, start)) = Strlen (s) -1) {strs.push_back (s.sstr (start, end-start)); Start = end + 1; }  

instead of

  while ((end = s.find (splitChar, start)) = String :: npos {strs.push_back ( ) S.substr (start, end-start)); Start = end + 1; }  

No comments:

Post a Comment