[C++] boost::function geeft foutmelding

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

Topicstarter
Zal wel iets stoms zijn, maar ik zie even niet waarom dit niet werkt:

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class A : public boost::enable_shared_from_this<A> {
public:
    typedef boost::shared_ptr<A> Ptr;
    typedef boost::function<void (Ptr)> Handler;

    A(Handler h) : m_h(h) {}
    ~A() {}

    void foo() {
        m_h(shared_from_this());
    }

    Handler m_h;
};

class B {
public:
    B() {}
    ~B() {}

    void handler(A::Ptr aptr) {
    }

    void bar() {
        // Volgende regel triggered de error
        A::Ptr a(new A(boost::bind(&B::handler, this)));
        a->foo();
    }
};

int main() {
    B b;
    b.bar();

    return 0;
}



Error:
code:
1
2
3
4
5
6
7
8
9
10
11
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test.d" -MT"test.d" -o"test.o" "../test.cpp"
/usr/include/boost/mem_fn.hpp: In member function &#8216;R& boost::_mfi::dm<R, T>::operator()(T*) const [with R = void ()(boost::shared_ptr<A>), T = B]&#8217;:
/usr/include/boost/bind.hpp:222:   instantiated from &#8216;R boost::_bi::list1<A1>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = void (&)(boost::shared_ptr<A>), F = boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, A = boost::_bi::list1<boost::shared_ptr<A>&>, A1 = boost::_bi::value<B*>]&#8217;
/usr/include/boost/bind/bind_template.hpp:32:   instantiated from &#8216;typename boost::_bi::result_traits<R, F>::type boost::_bi::bind_t<R, F, L>::operator()(A1&) [with A1 = boost::shared_ptr<A>, R = void (&)(boost::shared_ptr<A>), F = boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, L = boost::_bi::list1<boost::_bi::value<B*> >]&#8217;
/usr/include/boost/function/function_template.hpp:152:   instantiated from &#8216;static void boost::detail::function::void_function_obj_invoker1<FunctionObj, R, T0>::invoke(boost::detail::function::function_buffer&, T0) [with FunctionObj = boost::_bi::bind_t<void (&)(boost::shared_ptr<A>), boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, boost::_bi::list1<boost::_bi::value<B*> > >, R = void, T0 = boost::shared_ptr<A>]&#8217;
/usr/include/boost/function/function_template.hpp:904:   instantiated from &#8216;void boost::function1<R, T1>::assign_to(Functor) [with Functor = boost::_bi::bind_t<void (&)(boost::shared_ptr<A>), boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, boost::_bi::list1<boost::_bi::value<B*> > >, R = void, T0 = boost::shared_ptr<A>]&#8217;
/usr/include/boost/function/function_template.hpp:720:   instantiated from &#8216;boost::function1<R, T1>::function1(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<void (&)(boost::shared_ptr<A>), boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, boost::_bi::list1<boost::_bi::value<B*> > >, R = void, T0 = boost::shared_ptr<A>]&#8217;
/usr/include/boost/function/function_template.hpp:1040:   instantiated from &#8216;boost::function<R ()(T0)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<void (&)(boost::shared_ptr<A>), boost::_mfi::dm<void ()(boost::shared_ptr<A>), B>, boost::_bi::list1<boost::_bi::value<B*> > >, R = void, T0 = boost::shared_ptr<A>]&#8217;
../test.cpp:31:   instantiated from here
/usr/include/boost/mem_fn.hpp:342: error: invalid use of non-static member function
make: *** [test.o] Error 1

Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

Topicstarter
Pfff.... nu ik het hier post zie ik het meteen |:(

Er mist een "_1" voor de functie parameter...
C++:
1
       A::Ptr a(new A(boost::bind(&B::handler, this, _1))); 

fixed het... stom. Kan dicht :)

Om er toch nog een beetje een topic van te maken:

Kan ik zoiets ook doen met een algemene templated functor ipv boost::function?

Dus:

C++:
1
2
3
4
template <typename Handler>
class A : public boost::enable_shared_from_this<A<Handler> > {
...
};

? Het probleem is dat ik dan (volgens mij) een recursieve template definitie krijg, want de handler moet het type van A weten, dan afhangt van de handler.

[ Voor 49% gewijzigd door Zoijar op 21-02-2009 12:17 ]


Acties:
  • 0 Henk 'm!

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 13-09 00:05
Dat mag in principe wel, google maar op "Curiously Reccuring Template Pattern".

En boost heeft duidelijk "concepts" nodig, ja. Missend argument hoort een compile-time fout te zijn.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


Acties:
  • 0 Henk 'm!

  • Zoijar
  • Registratie: September 2001
  • Niet online

Zoijar

Because he doesn't row...

Topicstarter
MSalters schreef op zaterdag 21 februari 2009 @ 14:37:
Dat mag in principe wel, google maar op "Curiously Reccuring Template Pattern".
CRTP ken ik wel (tenminste, in de inheritance vorm); ik bedoelde zoiets:

C++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <typename Handler>
class P {
public:
    P(Handler h) {
        h(this);
    }
    ~P() {}
};

void handle_func(P<???>* p) {
}


int main() {
    P<???> p(&handle_func);

    return 0;
}

Ik snapte niet wat er dan op de plekken van de "???"s moest staan. Kan zoiets? Slecht ontwerp denk ik :)

[ Voor 3% gewijzigd door Zoijar op 21-02-2009 16:25 ]