【发布时间】:2019-09-16 15:46:52
【问题描述】:
我试图在 for 循环中访问两个不同向量之间的多个元素。 Visual Studio 给我以下警告 C26451;
算术溢出:对 4 字节值使用运算符“+”,然后将结果转换为 8 字节值。在调用运算符 '+' 之前将值转换为更广泛的类型以避免溢出 (io.2)。
我尝试过转换各种数据类型,但我知道我应该使用迭代器在循环中通过向量移动,但是,因为我在循环中使用两个向量,并且一次为每个向量使用多个元素我找不到正确实现这一点的方法。 这是我遇到相同问题的两个不同功能;
第一个函数;
Mat drawRails(Mat draw, vector<Point>lLines, vector<Point>rLines) {
//draw rails to the input image
for (int j = 0; j < lLines.size() - 1; j++) {
//draw rails - accessing point j and next point to correctly define the line
line(draw, lLines[j], lLines[j + 1], Scalar(255, 255, 255), 4);
line(draw, rLines[j], rLines[j + 1], Scalar(255, 255, 255), 4);
}
return draw;
}
第二个功能;
Mat drawHazardLines(Mat draw, vector<Point>lLines, vector<Point>rLines, int frameNum) {
//draw hazard lines to track
for (int j = 0; j < lLines.size() - 1; j++) {
//draw outwards moving rail lines - divide rail width by ten and multiply by modulo 10 of frame to achieve motion
int railDistNext = (rLines[j + 1].x - lLines[j + 1].x) / 10 * (frameNum % 10) + 2;
int railDist = (rLines[j].x - lLines[j].x) / 10 * (frameNum % 10) + 2;
Point Low, High;
Low = Point(lLines[j].x - railDist, lLines[j].y);
High = Point(lLines[j + 1].x - railDistNext, lLines[j + 1].y);
line(draw, Low, High, Scalar(0, 0, 255), 4);
Low = Point(rLines[j].x + railDist, rLines[j].y);
High = Point(rLines[j + 1].x + railDistNext, rLines[j + 1].y);
line(draw, Low, High, Scalar(0, 0, 255), 4);
}
return draw;
}
代码运行良好,但产生了我想解决的上述错误
【问题讨论】:
-
不相关,这些向量似乎都没有被任何一个函数实际修改。为什么你不通过 const-reference 传递它们是一个谜。对copy-ctor进行压力测试?仅供参考,
int j = 0; j < lLines.size() - 1是一个糟糕的主意。如果输入向量实际上是空的,你确实不想要那个计算;结果将是最不愉快的。使用迭代器。 -
算术溢出的错误/警告与在循环中迭代两个向量无关。尝试缩小您提出的两个问题中您真正需要帮助的范围。如果您在这两个方面都需要帮助,请提出单独的问题。将它们按您的方式组合在一起 - 以及不提供minimal reproducible example - 会降低获得任何有用建议的机会。
标签: c++ visual-studio opencv vector stdvector