473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to invoke member functions of an object in set

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;
}

Aug 3 '06 #1
3 1312
In article <11**********************@b28g2000cwb.googlegroups .com>,
xiaohuamao <ca**************@gmail.comwrote:
>Hi,all. I am a beginner in STL.
Try the C++ newsgroup.

>#include <set>
Is that sufficient to define cout, endl, and so on? It wouldn't
be enough in C.
--
All is vanity. -- Ecclesiastes
Aug 3 '06 #2
Sorry, i steped into the wrong group. Thank you for you help.

Aug 3 '06 #3
xiaohuamao wrote:
Hi,all. I am a beginner in STL.
STL (and your code) is in C++, not C. While there is an ancestral link,
C and C++ split apart before there was even a standard for either.
These languages are sufficiently different and have sufficiently
different attitudes about the right ways to do things, that it makes no
sense to ask about C++ in <news:comp.lang.c>. The good news is that
there is <news:comp.lang.c++for people who have C++ questions and who
before posting are willing to check the FAQ and follow that newsgroup
for several weeks (which can be done in much less time with the archive
at Google groups).

An example of why you should follow those simple rules of etiquette in
newsgroups: had you checked the FAQ for this newsgroup or followed it
for any time, you would have known not to post here and not wasted your
time.
Aug 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
30
by: Joost Ronkes Agerbeek | last post by:
Why is it allowed in C++ to call a static member function of an object through an instance of that object? Is it just convenience? tia, Joost Ronkes Agerbeek
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
12
by: Anthony Jones | last post by:
Just a bit of background: I'm one of a group of FORTRAN programmers, looking to switch to C++. We are trying to write a few simple examples to demonstrate the power of the language to our manager,...
22
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void...
11
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.