473,385 Members | 1,409 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,385 software developers and data experts.

mem_fun error

I am getting a compile error on any compiler I try, so I know I have
an error here. Can anyone see it?

//includes

class Foo
{
public:
Foo(int a){m_hi = a;}
int hi(){return m_hi;}

int m_hi;
};

int main()
{
std::vector<Foo *data;
data.push_back(new Foo(3));

max_element(data.begin(),
data.end(),
std::mem_fun(&Foo::hi));

}

gives a compile error:
in gcc: stl_algo.h:4565: error: no match for call to
'(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
stl_function.h:600: note: candidates are: _Ret
std::mem_fun_t<_Ret,_Tp>::operator()(_Tp*) const [with _Ret = int,
_Tp = Foo]

help!

}

Mar 7 '07 #1
4 2575
joseph cook wrote:
I am getting a compile error on any compiler I try, so I know I have
an error here. Can anyone see it?

//includes
There aren't any....

--
Ian Collins.
Mar 7 '07 #2
//includes

There aren't any....
I meant "all neccessary includes are being included".

To be pendantic about it:
#include <functional>
#include <algorithm>
#include <vector>

are at the top of my file

Mar 7 '07 #3
On Mar 7, 4:56 pm, "joseph cook" <joec...@gmail.comwrote:
I am getting a compile error on any compiler I try, so I know I have
an error here. Can anyone see it?

//includes

class Foo
{
public:
Foo(int a){m_hi = a;}
int hi(){return m_hi;}

int m_hi;

};

int main()
{
std::vector<Foo *data;
data.push_back(new Foo(3));

max_element(data.begin(),
data.end(),
std::mem_fun(&Foo::hi));

}

gives a compile error:
in gcc: stl_algo.h:4565: error: no match for call to
'(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
stl_function.h:600: note: candidates are: _Ret
std::mem_fun_t<_Ret,_Tp>::operator()(_Tp*) const [with _Ret = int,
_Tp = Foo]

help!

}
You're specifying a member function adapter where you need a
comparator. A member function is invoked like:

some_mem_fun( *i )

where some_mem_fun is the functor resulting from your mem_fun call and
i is an iterator in the range [begin, end). Note that the functor only
takes one parameter. A comparator, on the other hand, has a signature
like:

bool less_than( a, b )

Probably what you intended is something like this:

for_each( data.begin(),
data.end(),
mem_fun(&Foo::hi));

or

int mx = numeric_limits<int>::min();
for( vector<Foo*>::const_iterator i=data.begin();
i != data.end(); ++i )
{
mx = max( (*i)->hi(), mx );
}

(Of course, this would require Foo::hi() to be a const function.)

Cheers! --M

Mar 7 '07 #4
On Mar 8, 6:56 am, "joseph cook" <joec...@gmail.comwrote:
I am getting a compile error on any compiler I try, so I know I have
an error here. Can anyone see it?

//includes

class Foo
{
public:
Foo(int a){m_hi = a;}
int hi(){return m_hi;}

int m_hi;

};

int main()
{
std::vector<Foo *data;
data.push_back(new Foo(3));

max_element(data.begin(),
data.end(),
std::mem_fun(&Foo::hi));

}

gives a compile error:
in gcc: stl_algo.h:4565: error: no match for call to
'(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
stl_function.h:600: note: candidates are: _Ret
std::mem_fun_t<_Ret,_Tp>::operator()(_Tp*) const [with _Ret = int,
_Tp = Foo]

help!

}- Hide quoted text -

- Show quoted text -
Foo::hi should be either static or Global which takes two arguments as
input and bool as return. now it's in the context of a class (object)

Mar 8 '07 #5

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

Similar topics

1
by: k0tic | last post by:
The intent of the following code is to call delete on each pointer in the vector dead implicitly via auto_ptr<T>'s reset method semantics which delete their current pointer before taking ownership...
5
by: Old Wolf | last post by:
I have a member function that acts on an object. I would also like to have a member function that acts on a container of such objects, using std::for_each. I tried: #include <algorithm>...
2
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
13
by: Ioannis Vranos | last post by:
What is the exact difference between mem_fun and mem_fun_ref, since in all examples I looked at, they are used in exactly the same way? -- Ioannis Vranos http://www23.brinkster.com/noicys
4
by: ShaneG | last post by:
We have ptr_fun to handle functions, mem_fun to handle member functions that will be called through a pointer, and mem_fun_ref to handle member functions that will be called through a reference. ...
2
by: joseph cook | last post by:
I am getting a compile error on any compiler I try, so I know I have an error here. Can anyone see it? //includes class Foo { public: Foo(int a){m_hi = a;} int hi(){return m_hi;}
1
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; class Test
8
by: flopbucket | last post by:
Hi, I am having some problem using std::bind1st() and mem_fun. I want to bind member function calls to some kind of functor so it can be called later. The following works fine for me: ...
1
by: subramanian100in | last post by:
Suppose I have class WordAndLineNumbers { public: void print( ); // other member functions private: // data members
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.