Mooophy/Cpp-Primer

Exercise 10_32.cc (G++ giving error on the accumulate algorithm)

yapkm01 opened this issue · 0 comments

As per your code, i am getting errors on the accumulate algorithm as shown below.

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <numeric>
    #include "include/Sales_data.h"
    using namespace std;
  
    int main() {

      istream_iterator<Sales_data> in_iter(cin), in_eof;
      vector<Sales_data> vec;
  
      while (in_iter != in_eof)
          vec.push_back(*in_iter++);
      sort(vec.begin(), vec.end(), compareIsbn);
  
      for (auto& v: vec) {
              cout << v << endl;
      }
    
      for (auto beg = vec.cbegin(), end = beg; beg != vec.cend(); beg = end) {
          end = find_if(beg, vec.cend(), [beg](const Sales_data &item){ return item.isbn() != beg->isbn(); });
          cout << accumulate(beg, end, Sales_data(beg->isbn())) << endl; // ERROR HERE!
      }

      return 0;

    }
 

Errors:
In template: invalid operands to binary expression ('typename std::remove_reference<Sales_data &>::type' ..

Can you please explain how you managed to compile without errors? I checked this algorithm, and i don't see any issue based on the algorithm available parameters as in the cppreference site.

https://en.cppreference.com/w/cpp/algorithm/accumulate