如何循环遍历两个向量

时间:2019-04-27 09:53:45

标签: c++ visual-studio opencv vector stdvector

我正在尝试访问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;
}

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

//for BookInfo:
public void addRow(Object[] objToAdd)
    {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(objToAdd);
    }
...

        JButton btnAdd = new JButton("Add");
        btnAdd.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, new Color(0, 0, 0), new Color(0, 0, 0)));
        btnAdd.setOpaque(true);
        btnAdd.setFont(new Font("American Typewriter", Font.PLAIN, 18));
        btnAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Reservation.reserve(/*Object bookID*/);
            }
        });
String[] columnNames = {" Book ID", " Title ", " Author", " Genre", " Date Published ", " Availability"}; // table layout

//for Reservation:
public void reserve(Object[] bookID)
    {
        String issuedDate = new SimpleDateFormat("yyyy.MM.dd").format(new Date());

        Calendar c = Calendar.getInstance();
        c.setTime(new Date()); 
        c.add(Calendar.DATE, 30); 
        String returnDate = new SimpleDateFormat("yyyy.MM.dd").format(c);


        String[]resInfo = {"Book ID" , issuedDate, returnDate};
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        model.addRow(resInfo);
    }

您当然需要确保rLines的项目至少与lLines一样多...