fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. struct printClass {
  8. int count;
  9.  
  10. printClass(int n) : count(n) {}
  11.  
  12. void operator()(int &i) const {
  13. count++;
  14. cout << i << "[" << count << "] ";
  15. }
  16. };
  17.  
  18. int main(int argc, char** argv) {
  19. vector<int> a(5, 7);
  20. a[4] = -1;
  21. a.resize(10, 3);
  22. int state = 0;
  23. for_each(a.rbegin(), a.rend(), printClass(state));
  24. }
  25.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘void printClass::operator()(int&) const’:
prog.cpp:13:12: error: increment of member ‘printClass::count’ in read-only object
stdout
Standard output is empty