Trouble understanding the std generate function

Hi guys,

New to c++. I'm trying to understand the following:
http://www.cplusplus.com/reference/algorithm/generate/

Especially when you are trying to generate a vector (or whatever std container) with a class that has several member variables. I believe that I need to feed the generate function a class that overloads operator(). I've got the following code...
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

class PancakeGlutton{
  public:
    void SetGluttonNumber(unsigned short int n) { gluttonNumber = n; }
    unsigned short int GetGluttonNumber() const { return gluttonNumber; }
    void SetPancakesEaten(unsigned int e) { pancakesEaten = e; }
    unsigned short int GetPancakesEaten() const { return pancakesEaten; }
	
  private:
    unsigned short int gluttonNumber;
    unsigned int pancakesEaten;
};

class GenClass{
  public:
    GenClass(vector <PancakeGlutton>::iterator & it){
      currentGlutton = 0;
      myIT = it;
    }
    ~GenClass() {};
		
    void operator()(){
      cout << "In operator() on currentGlutton: " << currentGlutton <<endl;
      myIT->SetGluttonNumber(currentGlutton);
      myIT->SetPancakesEaten(rand());
      currentGlutton++;
      myIT++;
   }
			
		
  private:
    int currentGlutton;
    vector <PancakeGlutton>::iterator myIT;
};

int main(){
	vector <PancakeGlutton> bfastParty(10);
	vector <PancakeGlutton>::iterator it;
	it = bfastParty.begin();
	GenClass uniqueGlutton(it);
	
	generate(bfastParty.begin(), bfastParty.end(), uniqueGlutton);
	
	for (it = bfastParty.begin(); it != bfastParty.end(); ++it){
		cout << "bfastParty gluttonNumbers: " << it->GetGluttonNumber() << ' ';
		cout << "bfastParty pancakesEaten: " << it->GetPancakesEaten() << ' ';
	}
	cout << endl;
	
	return 0;
}


I, however, get an error message when I call generate. It is:
C:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h In function `void std::generate(_ForwardIterator, _ForwardIterator, _Generator) [with _ForwardIterator = __gnu_cxx::__normal_iterator<PancakeGlutton*, std::vector<PancakeGlutton, std::allocator<PancakeGlutton> > >, _Generator = GenClass]':
46 C:\fac\pancakeGlutton.cpp instantiated from here


Any help would be greatly appreciated!!
I got it. For anyone else interested. I needed to return a PancakeGlutton from the operator() overload, not void. Also there were a few logic errors that I had to work out. Funny how after thinking about it for a day, then posting, then it pops into my head. So it goes.

Thanks!
Topic archived. No new replies allowed.