Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...