【发布时间】:2019-11-09 15:53:02
【问题描述】:
我正在尝试从calculate_markerVectors 函数返回向量并将其复制到pose,但我一直遇到“向量下标超出范围”错误。
我可以尝试通过引用返回,但我宁愿知道我在使用这种函数返回时做错了什么。
返回值由 3 个double 类型元素组成。
以下是部分代码:
cv::Point2d pt(1,1);
bool new_Point = false;
int prog(){
vector<vector<cv::Point2f>> markerCorners;
//.....
if (new_Point == true) {
vector <double> pose = calculate_markerVectors(markerCorners, pt); // HERE ERROR OCCURS
cout << "MARKER POSE" << pose[0] << "," << pose[1] << endl;
}
//.....
}
vector <double> calculate_markerVectors(vector<vector<cv::Point2f>> corners, cv::Point2d clickPt)
{
//some calulation happens here
cv::Point2f S;
S.x = corners[0][2].x - (vec_AC[0] / 2);
S.y = corners[0][2].y - (vec_AC[1] / 2);
double theta = acos(dot_prod / (scalarSF*scalarST));
vector <double> pos_img;
pos_img.push_back(S.x);
pos_img.push_back(S.y);
pos_img.push_back(theta);
return pos_img;
}
【问题讨论】:
-
markerCorners填写是否正确? -
您应该尝试使用调试器。 Visual Studio 有一个很棒的调试器。
-
您在显示的代码中返回向量的方式没有任何问题。代码
//some calculation happens here中有返回错误情况吗? -
@fdan 是的。它有 1 个元素,它是具有 4 个元素的向量。
-
我会在您访问姿势元素 [0] 和 [1] 之前添加一个长度检查,并尝试确定何时返回的矢量元素太少。