Issues with boost::function

S

Sanatan

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
 
F

Frank Neuhaus

Sanatan said:
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
 
S

Sanatan

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top