c++ - 我的 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;
}
解决方案
您声明(连同定义)接收到对双精度向量的两个引用。
double overlap(vector<double> &R1, vector<double> &R2)
{ /* ... */ }
你用两个 int 向量调用
vector<int> R1;
vector<int> R2;
/* ... */ overlap(R1, R2) /* ... */
如果您仔细阅读错误消息,我敢打赌它会告诉您这一点。
推荐阅读
- javascript - 用 JS 去除引号以转换为 JSON 对象
- python - python在所有大写单词上拆分字符串
- python - 我试图在后台添加一个倒数计时器,以便在时间用完时终止程序
- api - 不可预测的 soundcloud api 曲目列表
- python - 如何平滑pyautogui的鼠标移动?
- kubespray - kubespray 是否安装任何日志轮换策略?
- delphi - 在组件安装期间调试包
- java - Android Studio 未在设备上安装应用程序
- sql - 条件案例和 LIKE/ILIKE
- java - SonarQube for Java:确保在此处安全使用命令行参数