0

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
}
acraig5075
  • 10,588
  • 3
  • 31
  • 50
Naveen Verma
  • 367
  • 1
  • 5
  • 18
  • @Evg Updated the question with the full error message. – Naveen Verma Nov 20 '19 at 12:39
  • 3
    Where is `getRect()` defined in the real code? Just before main or in some `.cpp` file? – Evg Nov 20 '19 at 12:40
  • It builds right to me. How do you have your files sorted? Where are those functions implemented/declared? Also you can use the same template parameter for each function, no need for T1, T2, ... – janonespe Nov 20 '19 at 12:41
  • @Evg I have just posted the implementation. In code, it is declared in the header file and defined in .cpp file – Naveen Verma Nov 20 '19 at 12:42
  • That's your problem. Follow the link above, it will help. – Evg Nov 20 '19 at 12:43
  • 1
    @NaveenVerma [The Ben's answer](https://stackoverflow.com/a/16493574/11455384) explains quite well what's happening. – Fareanor Nov 20 '19 at 12:50

0 Answers0