mem_fun error

J

joseph cook

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>::eek:perator()(_Tp*) const [with _Ret = int,
_Tp = Foo]

help!

}
 
V

Victor Bazarov

joseph said:
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;}

This is not a proper comparison function.
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));

The third argument needs to be a comparison function with a very
specific signature. Think how your 'hi' should be used with TWO
arguments of type Foo*.
}

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>::eek:perator()(_Tp*) const [with _Ret = int,
_Tp = Foo]

help!

}

V
 
J

joseph cook

The third argument needs to be a comparison function with a very
specific signature. Think how your 'hi' should be used with TWO
arguments of type Foo*.

Of course; thank 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

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top