You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++新手求助:overlap函数报错,请求排查代码问题

Hey there! Let's walk through the issues in your code step by step—there are a few small but critical problems causing the errors:

1. Mismatched Vector Types

Your overlap function expects references to vector<double>, but in main you're creating vector<int> and passing those in. C++ won’t implicitly convert vector<int> to vector<double>, so this is a clear compile error. You can either adjust the function parameters to accept vector<int>&, or create vector<double> instances in main instead.

2. Incorrect Rectangle Boundary Logic

Looking at your comments, you’re storing width and height as the third and fourth elements of your vectors—but your code treats those values as the right and top boundary coordinates directly. That’s wrong! For a rectangle with origin (x1, y1), width w1, and height h1, the right boundary should be x1 + w1 and the top boundary should be y1 + h1. Your current code uses R1[2] (width) as the right x-coordinate, which only works if x1 is 0—for any other origin, this will miscalculate the overlap entirely.

3. Missing Semicolon

The line where you calculate overlap_area is missing a semicolon at the end:

double overlap_area = (xmax-xmin)*(ymax-ymin) // <-- No semicolon here!

This will trigger a syntax error during compilation.

4. Missing Required Headers & Namespace

Your code doesn’t include the necessary headers for vector, cout, and the max/min functions. You also need to either use std:: prefixes for standard library elements or add using namespace std; to avoid naming errors.


Here’s the fixed version of your code with all issues resolved:

#include <iostream>
#include <vector>
#include <algorithm> // For std::max and std::min

using namespace std;

double overlap(vector<double> &R1, vector<double> &R2) {
    // x轴方向的重叠部分
    double xmin = max(R1[0], R2[0]);
    double xmax = min(R1[2], R2[2]);
    if (xmin >= xmax) return 0;
    // y轴方向的重叠部分
    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); // Fixed semicolon
    return overlap_area;
}

int main() {
    vector<double> R1;
    vector<double> R2;
    // 矩形坐标
    // 使用.push_back()函数将矩形参数添加至向量,供算法使用
    // x:x坐标,y:y坐标,w:宽度,h:高度(w、h为矩形左/底到右/顶的距离)
    int x1 = 0, y1 = 0, w1 = 6, h1 = 9;
    R1.push_back(x1);
    R1.push_back(y1);
    R1.push_back(x1 + w1); // 存储右边界而非宽度
    R1.push_back(y1 + h1); // 存储上边界而非高度
    int x2 = 3, y2 = 4, w2 = 3, h2 = 3;
    R2.push_back(x2);
    R2.push_back(y2);
    R2.push_back(x2 + w2); // 存储右边界而非宽度
    R2.push_back(y2 + h2); // 存储上边界而非高度
    cout << "Overlap = " << overlap(R1, R2) << endl;
    return 0;
}

If you’d rather keep using vector<int> in main, just adjust the overlap function’s parameters to vector<int>& (C++ will implicitly convert int values to double for the calculations):

double overlap(vector<int> &R1, vector<int> &R2) {
    double xmin = max(R1[0], R2[0]);
    double xmax = min(R1[2], R2[2]);
    if (xmin >= xmax) return 0;
    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;
}

This should compile and correctly calculate the overlap area (which for your test case is 9, by the way!).

内容的提问来源于stack exchange,提问作者Moe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 20:02:54