By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

mem_fun error

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
//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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.