Thread: Using boost::bind - error, help?

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Using boost::bind - error, help?

    I just tried to do:
    Code:
    boost::bind(&Input<std::string>::ReadFunction, InputClass),
    While
    InputClass is: Input InputClass,
    Input is a template class: template<typename StrT> class Input,
    and ReadFunction is a member function of the class that takes one parameter: void ReadFunction(StrT& RawInput)

    The idea is to pass the bound boost object to a member function of the class, like so:
    Code:
    if ( InputClass.ReadData
    	(
    		ConvertedInput,
    		boost::bind(&Input<std::string>::ReadFunction, InputClass),
    		&boost::lexical_cast<DataT, std::string>
    	) )
    And allow the class to call the boost object as if it were a global function or a functor:
    Code:
    InputFunction(RawInput);
    But clearly I am missing something, because I am getting a compile error in boost::bind:
    3>d:\w00t\documents\visual studio 2008\projects\boost\boost\mem_fn.hpp(342) : error C2298: 'return' : illegal operation on pointer to member function expression
    3> d:\w00t\documents\visual studio 2008\projects\boost\boost\mem_fn.hpp(341) : while compiling class template member function 'void (&boost::_mfi::dm<R,T>::operator ()(T *) const)'
    3> with
    3> [
    3> R=void (std::string &),
    3> T=Input::Input<std::string>
    3> ]
    3> d:\w00t\documents\visual studio 2008\projects\boost\boost\bind\bind_template.hpp(3 44) : see reference to class template instantiation 'boost::_mfi::dm<R,T>' being compiled
    3> with
    3> [
    3> R=void (std::string &),
    3> T=Input::Input<std::string>
    3> ]
    3> d:\w00t\documents\visual studio 2008\projects\temp\temp3.cpp(154) : see reference to class template instantiation 'boost::_bi::bind_t<R,F,L>' being compiled
    3> with
    3> [
    3> R=void (&)(std::string &),
    3> F=boost::_mfi::dm<void (std::string &),Input::Input<std::string>>,
    3> L=boost::_bi::list1<boost::_bi::value<Input::Input <std::string> >>
    3> ]
    3> d:\w00t\documents\visual studio 2008\projects\temp\temp3.cpp(216) : see reference to function template instantiation 'void Input::Read<int,const char[19],const char[40],boost::basic_format<Ch>,Input::Validate::Max>(Dat aT &,StrT1 (&),StrT2 (&),const StrT3 &,Validator)' being compiled
    3> with
    3> [
    3> Ch=char,
    3> DataT=int,
    3> StrT1=const char [19],
    3> StrT2=const char [40],
    3> StrT3=boost::basic_format<char>,
    3> Validator=Input::Validate::Max
    3> ]
    Function and code where this goes wrong:
    Code:
        R const & operator()(T const & t) const
        {
            return (t.*f_);
        }
    Any ideas what's wrong?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, of course. I have to specify a placeholder since the function expects an argument.
    boost::bind(&Input<std::string>::ReadFunction, InputClass, _1)
    This compiles. It just goes to show my inexperience with boost::bind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Oh, of course. I have to specify a placeholder since the function expects an argument.
    boost::bind(&Input<std::string>::ReadFunction, InputClass, _1)
    This compiles. It just goes to show my inexperience with boost::bind.
    For future reference, bind can also give obscure errors if it is unable to determine the return type of the object (function or functor) being bound. In that case the return type T must be specified with bind<T>(...)

    You'll know it when you see it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Correct way to call boost::bind?
    By jmd15 in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2008, 03:24 PM