iterator error in generic function

Hi

I attempting to use a generic function that iterates through a container from a start to end, matching an element and counting the number of times the match succeeds.
I get the following error thoughstl_iterator_sample.cpp:7:5: note: template argument deduction/substitution failed:

Can anybody suggest what is specified incorrectly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

template <typename Iterator, typename Elem>
int cnt(Iterator & start, const Iterator & finish, const Elem & v) {
  int n = 0; 
  for ( ; start != finish; ++start) {
    if (*start == v)
      n++;
  }
  return n;
}

struct Book {
  std::string title;
  int pages;
};

bool operator==(const Book & r1, const Book & r2) {
  if (r1.pages == r2.pages)
    return true;
  return false;
}

int main() {
  using std::cout;
  using std::vector;
  vector<Book> books;
  Book temp;
  typedef vector<Book>::iterator iter;

  cout << "Count " << cnt<iter, Book>(begin(books),end(books), Book{"Monsoon",10}) << "\n";

  return 0;
}

Line 7:
change:
 
int cnt(Iterator & start, const Iterator & finish, const Elem & v) {
to
 
int cnt(Iterator start, const Iterator & finish, const Elem & v) {


(cannot change the value of begin(books)).
Topic archived. No new replies allowed.