Confusing “std::out_of_range” Error

こ雲淡風輕ζ 提交于 2019-12-12 06:57:59

问题


I've been debugging this error for the last 2 hours, and knowing myself I won't be able to sleep if I don't ask for help before I go to bed. I'm writing a model loader for my game, and just for now I'm using a pretty flimsy method to split strings. However, it works on nearly identical lines, then randomly doesn't. I'm using string.substr(), and I believe the error means its trying to start at a location that doesn't exist in the string. The call stack says its happening on this line:

v1 = v1.substr(s.find(",")+1);

and by using a breakpoint that prints a message, it says

Vertex 1 is using "1,1" and the entire string is "173,1,1 175,1,1 174,1,1"

where Vertex 1 is the value of v1, and string is the value of s.

This is the entire function:

FaceData data;
s = s.substr(5); //remove "FACE "
string v1, v2, v3;

//vertex 1
v1 = s.substr(0, s.find(" "));

data.vertexIndexes[0] = atoi(v1.substr(0, s.find(",")).c_str());
v1 = v1.substr(s.find(",")+1);
data.textureIndexes[0] = atoi(v1.substr(0, s.find(",")).c_str());
v1 = v1.substr(s.find(",")+1);
data.normalIndexes[0] = atoi(v1.c_str());

//vertex 2
s = s.substr(s.find(" ")+1);
v2 = s.substr(0, s.find(" "));

data.vertexIndexes[1] = atoi(v2.substr(0, s.find(",")).c_str());
v2 = v2.substr(s.find(",")+1);
data.textureIndexes[1] = atoi(v2.substr(0, s.find(",")).c_str());
v2 = v2.substr(s.find(",")+1);
data.normalIndexes[1] = atoi(v2.c_str());

//vertex 3
s = s.substr(s.find(" ")+1);
v3 = s;

data.vertexIndexes[2] = atoi(v3.substr(0, s.find(",")).c_str());
v3 = v3.substr(s.find(",")+1);
data.textureIndexes[2] = atoi(v3.substr(0, s.find(",")).c_str());
v3 = v3.substr(s.find(",")+1);
data.normalIndexes[2] = atoi(v3.c_str());

return data;

the std::string 's' being passed to the function always looks like this: "FACE X,X,X X,X,X X,X,X" where x is a number.

This is the only way I could find to split a string...

Now, I don't understand why it is getting this error here... It seems like it's almost just happening randomly. I can't understand why it won't work with

173,1,1 175,1,1 174,1,1

but it will work with

175,2,2 176,2,2 175,2,2


回答1:


I'm not sure I'm interpreting your question correctly, but given the information you provided, this seems to be what you're doing:

#include <string>
#include <iostream>

int main() {
    std::string v1 = "1,1";
    std::string s = "173,1,1 175,1,1 174,1,1";

    try {
        v1 = v1.substr(s.find(",")+1);
    }
    catch (const std::out_of_range& e) {
        std::cout << "out_of_range: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

In that case, s.find(",") will return 3 (the first , in s is at position 3), however since v1 only has three characters the only valid indexes are between [0,2]. Passing in 3, or with the +1 4 would be out of range for v1.



来源:https://stackoverflow.com/questions/8549338/confusing-stdout-of-range-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!