Hi,all. I am a beginner in STL. When i compile the following code, i
meet an error.
The error code is "error C2662: 'vPrint' : cannot convert 'this'
pointer from 'const class son' to 'class son &'.
What should i do to make it work? Remove the constness? How to?
#include <set>
using namespace std;
class son {
private:
int i;
public:
son(int num) : i(num) {}
int i4GetNum() {return i;}
void vPrint() {cout << "Hi,Son " << i << " !" << endl;}
};
struct sonCmp : public binary_function<son, son, bool>{
bool operator () (son n1, son n2) const{
return n1.i4GetNum() < n2.i4GetNum();
}
};
int main()
{
set<son, sonCmpsetSon;
set<son, sonCmp>::iterator iter;
for (int i = 0; i<10; i++)
setSon.insert(son(i));
// for_each(setSon.begin(), setSon.end(), mem_fun_ref((&son::vPrint)));
for (iter = setSon.begin(); iter != setSon.end(); ++iter)
iter->vPrint();
return;
}