1

I tried the example code in this book to draw the contours in the original picture. However, the following code won't compile successfully under Qt with Mingw 4.4.

 // Eliminate too short or too long contours
   int cmin= 100;  // minimum contour length
   int cmax= 1000; // maximum contour length
   std::vector<std::vector<cv::Point> >::
              const_iterator itc= contours.begin();
   while (itc!=contours.end()) {
      if (itc->size() < cmin || itc->size() > cmax)
         itc= contours.erase(itc);
      else 
         ++itc;
   }

Warning:comparison between signed and unsigned integer expressions Warning:comparison between signed and unsigned integer expressions Error:no matching function for call to 'std::vector, std::allocator > >, std::allocator, std::allocator > > > >::erase(__gnu_cxx::__normal_iterator, std::allocator > >*, std::vector, std::allocator > >, std::allocator, std::allocator > > > > >&)'

It says that the itc doesn't have the method size(). However, the book really writes like that. Did I miss something?

4

1 回答 1

2

那是因为std::vector::erase返回 an iterator,并且您分配给 a const_iterator。这编译:

...
std::vector<std::vector<cv::Point> >::iterator itc= contours.begin();
                                       // ^
while (itc!=contours.end()) {
   if (itc->size() < cmin || itc->size() > cmax)
       itc= contours.erase(itc);
   else 
       ++itc;
}
于 2012-07-03T13:26:22.097 回答