混淆“std :: out_of_range”错误 [英] Confusing "std::out_of_range" Error

查看:133
本文介绍了混淆“std :: out_of_range”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在调试这个错误的最后2个小时,知道自己,我不能睡觉,如果我不上床睡觉前请求帮助。我为我的游戏写了一个模型加载器,现在我使用一个漂亮的脆弱的方法来拆分字符串。然而,它工作在几乎相同的行,然后随机不。我使用string.substr(),我相信错误意味着它试图在字符串中不存在的位置开始。调用堆栈说明它发生在这一行:

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


顶点1使用1,1,整个字符串是173,1,1 175,1,1
174,1,1 p>

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

其中Vertex 1是v1的值,string是s的值。

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

这是整个函数:

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;

传递给函数的std :: string's'总是如下所示:
FACE X,X,XX,X,XX,X,X
其中x是一个数字。

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.

分割字符串...

现在,我不明白为什么它在这里得到这个错误...它似乎几乎只是随机发生。我不明白为什么它不能使用

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

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

但它适用于


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

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


推荐答案

不确定我是否正确解释您的问题,但根据您提供的信息,这似乎是您正在做的:

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;
}

在这种情况下, s.find )将返回 3 位于位置3),然而由于 v1 只有三个字符,所以唯一有效的索引在 [0,2] 。传入 3 或与 +1 4 超出 v1 的范围。

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.

这篇关于混淆“std :: out_of_range”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆