mem_fun and C2784 using Ms VC++

M

Mr X

Can anyone tell how to fix the compile error for the program below?

#include "iostream.h"
#include <vector>
#include <algorithm>
#include <functional>
#include <list>

using namespace std;

class Widget
{
};

class B
{
public:
bool IsInteresting(Widget) {return true;}
};

class C
{
public:
void Find(B b)
{
list<Widget>::iterator i = find_if(m_widgets.begin(),m_widgets.end(),
mem_fun_ref((*this).IsInteresting));
}
private:
list<Widget>m_widgets;
bool IsInteresting(const Widget widget) {return true;}
};

void main(void)
{};
 
V

Victor Bazarov

Mr said:
Can anyone tell how to fix the compile error for the program below?

#include "iostream.h"'

Ugh!

#include said:
#include <vector>
#include <algorithm>
#include <functional>
#include <list>

using namespace std;

class Widget
{
};

class B
{
public:
bool IsInteresting(Widget) {return true;}
};

class C
{
public:
void Find(B b)
{
list<Widget>::iterator i = find_if(m_widgets.begin(),m_widgets.end(),
mem_fun_ref((*this).IsInteresting));

Shouldn't this be

mem_fun_ref(&C::IsInteresting)

?
}
private:
list<Widget>m_widgets;
bool IsInteresting(const Widget widget) {return true;}
};

void main(void)
Yuck!

{};

Bleh!
 
M

Mr X

This doesn't work either: (can someone come up with something that
does?)

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <list>

using namespace std;

class Widget
{};

class B
{};

class C
{
public:
void Find(B b)
{
Widget widget;
std::find_if(m_widgets.begin(),m_widgets.end(),std::bind2nd(std::mem_fun(&C::IsInteresting),widget));
}
private:
bool IsInteresting(Widget) {return true;}
list<Widget>m_widgets;
};
 
V

Victor Bazarov

Mr said:
This doesn't work either: (can someone come up with something that
does?)

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <list>

using namespace std;

class Widget
{};

class B
{};

class C
{
public:
void Find(B b)
{
Widget widget;
std::find_if(m_widgets.begin(),m_widgets.end(),std::bind2nd(std::mem_fun(&C::IsInteresting),widget));

Not sure what you're trying to accomplish, but this:

std::find_if(m_widgets.begin(),
m_widgets.end(),
std::bind1st(std::mem_fun(&C::IsInteresting),this));

should at least compile.
}
private:
bool IsInteresting(Widget) {return true;}
list<Widget>m_widgets;
};

V
 
M

Mr X

It doesn't :
error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
(__thiscall _Ty::*)(void))' : could not deduce template argument for
'<Unknown>
' from 'bool (__thiscall C::*)(class Widget)'
 
V

Victor Bazarov

Mr said:
It doesn't :
error C2784: 'class std::mem_fun_t<_R,_Ty> __cdecl std::mem_fun(_R
(__thiscall _Ty::*)(void))' : could not deduce template argument for
'<Unknown>
' from 'bool (__thiscall C::*)(class Widget)'

Get a better compiler.
 
V

Victor Bazarov

puzzlecracker said:
Can someone explain bind2st/bind1st stl functions?

What to explain? "Use the Source, Luke!" -- just look at the
'functional' header and find the source for those functions. They
are _literally_ one-liners.

Besides, what book on templates are you reading that doesn't talk
about bind1st/2nd?
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top