gpt4 book ai didi

c++ - 如何在循环中迭代两个 vector

转载 作者:行者123 更新时间:2023-11-28 01:21:45 25 4
gpt4 key购买 nike

我正在尝试访问 for 循环中两个不同 vector 之间的多个元素。 Visual Studio 给我以下警告 C26451;

算术溢出:对 4 字节值使用运算符“+”,然后将结果转换为 8 字节值。在调用运算符“+”之前将值转换为更宽的类型以避免溢出 (io.2)。

我尝试过转换各种数据类型,但我明白我应该使用迭代器在循环中移动一个 vector ,然而,因为我在循环中使用两个 vector ,并且一次为每个 vector 使用多个元素我找不到正确实现此方法的方法。这里有两个不同的函数,我在其中遇到了同样的问题;

第一个函数;

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

代码工作正常但是产生了我想解决的上述错误

最佳答案

该错误表示存在从longint 的转换。从您提供的代码很难判断错误出在哪里,但我建议在您的代码中将 int 更改为 long

关于c++ - 如何在循环中迭代两个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55879295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com