How to invoke member functions of the objects in a Set

  • Thread starter cainiaodelixiang
  • Start date
C

cainiaodelixiang

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>
#include <iostream>
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, sonCmp> setSon;
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;
}
 
V

Victor Bazarov

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?

No, not "remove", add.
#include <set>
#include <iostream>
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;}

Declare this function 'const':

void vPrint() const { ...

V
 
C

cainiaodelixiang

Thank you for your help.
There is still a a long way for me to go, i will do my best.
Thank you again.
Victor said:
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?

No, not "remove", add.
#include <set>
#include <iostream>
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;}

Declare this function 'const':

void vPrint() const { ...

V
 
T

tragomaskhalos

Thank you for your help.
There is still a a long way for me to go, i will do my best.
Thank you again.

Not related to your original question, but "i4GetNum" looks like the
most horrible bit of Hungarian notation I've ever seen. Ditto the v- in
"vPrint". If this is your idea, I urge you to cease and desist
immediately - it adds nothing but clutter to your code. If you are
working from a coding standard that mandates it, agitate to get it
changed asap. Just my advice.
 
H

Howard

tragomaskhalos said:
Not related to your original question, but "i4GetNum" looks like the
most horrible bit of Hungarian notation I've ever seen.

I thought it was text-message-speak, meaning "I forget the number! Can you
please tell me?" :)
 
M

Martijn van Buul

It occurred to me that tragomaskhalos wrote in comp.lang.c++:
Not related to your original question, but "i4GetNum" looks like the
most horrible bit of Hungarian notation I've ever seen.

Then you obviously haven't seen a lot of it, have you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top