首页 > 解决方案 > 我的 c++ 函数没有运行,我无法对其进行解码

问题描述

嘿伙计们,我是 C++ 新手。我在 C++ 中有以下函数,但它总是通过“双重重叠”函数给我一个错误,我不知道为什么。有人可以告诉我,我做错了什么吗?

double overlap(vector<double> &R1, vector<double> &R2) {
    // overlap in the x dimension
    double xmin = max(R1[0], R2[0]);
    double xmax = min(R1[2], R2[2]);
    if (xmin >= xmax) return 0;

    // overlap in the y dimension
    double ymin = max(R1[1], R2[1]);
    double ymax = min(R1[3], R2[3]);
    if (ymin >= ymax) return 0;

    double overlap_area = (xmax-xmin)*(ymax-ymin)

    return overlap_area;
}

int main() {
    vector<int> R1;
    vector<int> R2;

    // Coordinates of the rectangles
    // with the .push_back() function we are adding the rectangles to our vector to give it to the algorithm.
    // x: x-coordinate, y: y-coordinate, w: weidth, h: height - (w,h: distance from left/botton to right/top side of the rectangle)
    int x1 = 0, y1 = 0, w1 = 6, h1 = 9;
    R1.push_back(x1);
    R1.push_back(y1);
    R1.push_back(w1);
    R1.push_back(h1);

    int x2 = 3, y2 = 4, w2 = 3, h2 = 3;
    R2.push_back(x2);
    R2.push_back(y2);
    R2.push_back(w2);
    R2.push_back(h2);



    cout << "Overlap = " << overlap(R1, R2) << endl;
}

标签: c++function

解决方案


您声明(连同定义)接收到对双精度向量的两个引用。

double overlap(vector<double> &R1, vector<double> &R2)
{ /* ... */ }

你用两个 int 向量调用

vector<int> R1;
vector<int> R2;
/* ... */ overlap(R1, R2) /* ... */

如果您仔细阅读错误消息,我敢打赌它会告诉您这一点。


推荐阅读