?
Solved

Boost::Bind cannot convert from 'boost::_bi::bind_t<R,F,L>'

Posted on 2011-05-03
7
Medium Priority
?
1,262 Views
Last Modified: 2012-05-11
I need some boost::bind help.  I'm simply out of my depth here.

#include <boost/bind.hpp>
typedef void (*MY_CALLBACK)(unsigned short);

class TestClass {
   TestClass();
   void OnEvent(unsigned short);
};

TestClass::TestClass() {
   MY_CALLBACK cb = boost::bind(&TestClass::OnEvent,this,_1);
}

void TestClass::OnEvent(unsigned short) { }

void main() {
}

generates this error

error C2440: 'initializing' : cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'MY_CALLBACK'
1>        with
1>        [
1>            R=void,
1>            F=boost::_mfi::mf1<void,TestClass,unsigned short>,
1>            L=boost::_bi::list2<boost::_bi::value<TestClass *>,boost::arg<1>>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
0
Comment
Question by:josgood
  • 5
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35515683
Have you tried

TestClass::TestClass() {
   MY_CALLBACK cb = (MY_CALLBACK)boost::bind(&TestClass::OnEvent,this,_1);
}

?
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 2000 total points
ID: 35515705
Bind doesn't return a function pointer, it returns a boost::function. In your case it returns boost::function1<void, unsigned short>
0
 
LVL 13

Author Comment

by:josgood
ID: 35515745
jkr,

The cast changes the error...
   MY_CALLBACK cb = (MY_CALLBACK)boost::bind(&TestClass::OnEvent,this,_1);

error C2440: 'type cast' : cannot convert from 'boost::_bi::bind_t<R,F,L>' to 'MY_CALLBACK'
1>        with
1>        [
1>            R=void,
1>            F=boost::_mfi::mf1<void,TestClass,unsigned short>,
1>            L=boost::_bi::list2<boost::_bi::value<TestClass *>,boost::arg<1>>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
josgood earned 0 total points
ID: 35515778
evilrx,

Ah.  I see that.
      boost::function1<void,unsigned short> f = boost::bind(&TestClass::OnEvent,this,_1);

That compiles just fine.  Now I have to see what I want to do with that.
0
 
LVL 13

Author Comment

by:josgood
ID: 35515833
Sorry.  I meant for your answer to be the solution and somehow messed it up.
0
 
LVL 13

Author Comment

by:josgood
ID: 35515841

I meant for EvilRx's answer to be the solution.
0
 
LVL 13

Author Closing Comment

by:josgood
ID: 35714872
Thank you.  I have some other thinking to do, but you have answered the question.

I appreciate your help.

Joe
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question