【发布时间】:2016-05-30 10:43:19
【问题描述】:
我有一个名为cpatches 的向量,它不是空的,并且至少包含一个对象cv::Point3i(0,0,0)。下面是我的代码,它给出了分段错误。代码运行良好,直到到达r2 = objects.front().y 行,然后出现分段错误并中止代码。
我正在尝试将由cv::Point3i(0,0,0) 分隔的对象分离到另一个名为objects 的向量。在排序中,compare_row 和 compare_col 工作得非常好。 (它分别返回a.y > b.y 和a.z > b.z)
代码如下。
for (int i = 0; i < cpatches.size(); i++) {
cv::Point3i npoint = cpatches[i];
std::vector<cv::Point3i> objects;
std::cout << npoint << std::endl;
if (npoint.x != 1 && npoint.y != 1 && npoint.z != 1) {
objects.push_back(npoint);
} else {
std::sort(objects.begin(), objects.end(), compare_row);
int r1, r2, c1, c2;
r2 = objects.front().y;
r1 = objects.back().y;
std::sort(objects.begin(), objects.end(), compare_col);
c2 = objects.front().y;
c1 = objects.back().y;
cv::rectangle(display_obstacles, cv::Point(r1, c1), cv::Point(r2, c2), CV_RGB(0, 255, 0));
std::cout << "hhh" << r1 << ", " << c1 << "; " << r2 << ", " << c2 << "; ";
objects.clear();
}
}
你能帮我确定调用向量的第一个和最后一个元素时犯了什么错误吗?
【问题讨论】:
-
当
npoint在x,y,z中有1时,objects中没有对象。 -
如果
cpatches中的第一个npoint要去else 分支怎么办?objects将是空的,在objects.front()你会得到段错误。
标签: c++ vector segmentation-fault