Issues with boost::function

Discussion in 'C++' started by Sanatan, Dec 16, 2010.

  1. Sanatan

    Sanatan Guest

    Hi,
    I am trying to implement a multicast callbach dispatcher using
    boost::function.
    My test programme looks like:

    #include <iostream>
    #include <list>
    #include <boost/function.hpp>
    #include <boost/bind.hpp>

    using namespace std;

    template <class T> class feed
    {
    list<boost::function<void (T)> > actions;
    typedef boost::function<void (T)> cbk;
    public:
    void deliver(T data)
    {
    for (typename list<boost::function<void (T)> >::iterator a =
    actions.begin();
    a != actions.end(); ++a)
    {
    (*a)(data);
    }
    }
    void register(boost::function<void (T)> act)
    {
    actions.push_bach(act);
    }
    };
    void callback(pair<double, long> data)
    {
    cout << "Data (" << data.first << "," << data.second << ")" << endl;
    }
    int main(int, char **)
    {
    feed<pair<double, long> > f;
    boost::function<void (pair<double, long>)> cbk = callback;
    f.register(cbk);
    f.deliver(make_pair<double, long>(10.33, 20));
    return 0;
    }

    I am trying to compile with:

    g++ -g -o testmcast testmcast.cpp

    which yields:

    sanat@fractal:~/at/research/src$ g++ -g -o testmcast testmcast.cpp
    testmcast.cpp:21: error: invalid declarator before ‘act’
    testmcast.cpp:21: error: expected `)' before ‘act’
    testmcast.cpp: In function ‘int main(int, char**)’:
    testmcast.cpp:34: error: expected unqualified-id before ‘register’
    testmcast.cpp:34: error: expected `;' before ‘register’

    This seems very strange!

    What am I doing wrong?

    Any insight would be appreciated.

    Thanks!

    --Sanatan
     
    Sanatan, Dec 16, 2010
    #1
    1. Advertising

  2. "Sanatan" <> schrieb im Newsbeitrag
    news:...
    > Hi,


    Hey

    > I am trying to implement a multicast callbach dispatcher using
    > boost::function.
    > My test programme looks like:
    >
    > #include <iostream>
    > #include <list>
    > #include <boost/function.hpp>
    > #include <boost/bind.hpp>
    >
    > using namespace std;
    >
    > template <class T> class feed
    > {
    > list<boost::function<void (T)> > actions;
    > typedef boost::function<void (T)> cbk;
    > public:
    > void deliver(T data)
    > {
    > for (typename list<boost::function<void (T)> >::iterator a =
    > actions.begin();
    > a != actions.end(); ++a)
    > {
    > (*a)(data);
    > }
    > }
    > void register(boost::function<void (T)> act)
    > {
    > actions.push_bach(act);
    > }
    > };
    > void callback(pair<double, long> data)
    > {
    > cout << "Data (" << data.first << "," << data.second << ")" << endl;
    > }
    > int main(int, char **)
    > {
    > feed<pair<double, long> > f;
    > boost::function<void (pair<double, long>)> cbk = callback;
    > f.register(cbk);
    > f.deliver(make_pair<double, long>(10.33, 20));
    > return 0;
    > }
    >
    > I am trying to compile with:
    >
    > g++ -g -o testmcast testmcast.cpp
    >
    > which yields:
    >
    > sanat@fractal:~/at/research/src$ g++ -g -o testmcast testmcast.cpp
    > testmcast.cpp:21: error: invalid declarator before ‘act’
    > testmcast.cpp:21: error: expected `)' before ‘act’
    > testmcast.cpp: In function ‘int main(int, char**)’:
    > testmcast.cpp:34: error: expected unqualified-id before ‘register’
    > testmcast.cpp:34: error: expected `;' before ‘register’
    >
    > This seems very strange!
    >
    > What am I doing wrong?
    >
    > Any insight would be appreciated.
    >
    > Thanks!
    >
    > --Sanatan


    1) You mispelled "push_back" as "push_bach"
    2) "register" is a C++ keyword as you can see in [1]. Rename your function
    and it compiles fine.

    Cheers
    Frank

    [1] http://www.cppreference.com/keywords/index.html
     
    Frank Neuhaus, Dec 16, 2010
    #2
    1. Advertising

  3. Sanatan

    Sanatan Guest

    On Dec 16, 3:51 pm, "Frank Neuhaus" <> wrote:
    > "Sanatan" <> schrieb im Newsbeitragnews:...
    >
    > > Hi,

    <stupid question snipped>
    >
    > > This seems very strange!

    >
    > > What am I doing wrong?

    >
    > > Any insight would be appreciated.

    >
    > > Thanks!

    >
    > > --Sanatan

    >
    > 1) You mispelled "push_back" as "push_bach"
    > 2) "register" is a C++ keyword as you can see in [1]. Rename your function
    > and it compiles fine.
    >
    > Cheers
    >    Frank
    >
    > [1]http://www.cppreference.com/keywords/index.html


    Indeed you're right!

    Now I feel stupid...

    Thanks much!

    --Sanatan
     
    Sanatan, Dec 16, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard Latter
    Replies:
    2
    Views:
    2,910
    Julie
    May 17, 2004
  2. Steve Knight
    Replies:
    2
    Views:
    763
    Steve Knight
    Oct 10, 2003
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,756
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    718
    Shooting
    Aug 29, 2006
  5. Christopher
    Replies:
    1
    Views:
    813
    Yakov Gerlovin
    Oct 5, 2011
Loading...

Share This Page