user957121
user957121

Reputation: 3076

Compile error with the Contours example of OpenCV 2 cookbook

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?

Upvotes: 1

Views: 927

Answers (1)

juanchopanza
juanchopanza

Reputation: 227558

That is because std::vector::erase returns an iterator, and you are assigning to a const_iterator. This compiles:

...
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;
}

Upvotes: 2

Related Questions