I want to implement a generic method that will take a vector of Points (Point can be of type integer, float or double) and return rectangle. (I am using OpenCV).
By reading the online tutorial of generics in c++ from http://www.cplusplus.com/doc/oldtutorial/templates/ I have implemented the above method but I am getting a compile-time error.
Error: Error LNK2001 unresolved external symbol "public: static class cv::Rect_<int> __cdecl getRect<int>(class std::vector<class cv::Point_<int>,class std::allocator<class cv::Point_<int> > >)" (??$getRect@H@CvUtil@i2v@@SA?AV?$Rect_@H@cv@@V?$vector@V?$Point_@H@cv@@V?$allocator@V?$Point_@H@cv@@@std@@@std@@@Z)
I am unable to understand this error. Can someone explain to me what could be wrong here?
Below is the code snippet of my implementation.
template<typename T>
struct XCoordSortX {
bool operator() (cv::Point_<T> pt1, cv::Point_<T> pt2) { return (pt1.x < pt2.x); }
};
template<typename T2>
struct XCoordSortY {
bool operator() (cv::Point_<T2> pt1, cv::Point_<T2> pt2) { return (pt1.y < pt2.y); }
};
template<class T3>
cv::Rect getRect(const std::vector<cv::Point_<T3>> points)
{
cv::Rect rect;
std::vector<cv::Point_<T3>> tempPoints(points);
XCoordSortX<T3> sortX;
std::sort(tempPoints.begin(), tempPoints.end(), sortX); // sorting by x coordinate
rect.x = tempPoints[0].x; // setting top left point as lowest x
rect.width = tempPoints[tempPoints.size() - 1].x - tempPoints[0].x; // width will be diff between largest and smallest
XCoordSortY<T3> sortY;
std::sort(tempPoints.begin(), tempPoints.end(), sortY);
rect.y = tempPoints[0].y; // setting top left point as lowest y
rect.height = tempPoints[tempPoints.size() - 1].y - tempPoints[0].y;// height will be diff between largest and smallest
return rect;
}
int main()
{
std::vector<cv::Point> pointVec;
pointVec.push_back(cv::Point(2, 3)); // cv::Point is cv::Point_<int> type
pointVec.push_back(cv::Point(2, 10));
pointVec.push_back(cv::Point(10, 3));
pointVec.push_back(cv::Point(12, 5));
pointVec.push_back(cv::Point(40, 50));
pointVec.push_back(cv::Point(40 ,100));
pointVec.push_back(cv::Point(100, 40));
pointVec.push_back(cv::Point(50,50));
cv::Rect newRoi = getRect<int>(pointVec); // Error here
}