我试图访问for循环中两个不同向量之间的几个元素。Visual给我以下警告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;
}但是,代码工作正常,但会产生上述错误,我想要解决这些错误。
发布于 2019-04-27 10:09:03
此错误意味着有从long到int的转换。从您提供的代码中很难看出错误在哪里,但我建议将代码中的int更改为long。
发布于 2019-04-27 10:05:34
for(auto lIter = lLines.begin(), rIter = rLines.begin(); liter != lLines.end(); ++lIter, ++rIter)
{
...
}当然,您需要确保rLines至少有和lLines一样多的项目.
https://stackoverflow.com/questions/55879295
复制相似问题