I am trying to pass a class constructor into ros subscribe function. Is it possible to have the constructor called when the topic is published to?
class udpMsg
{
public:
std::string udp;
size_t posOfDelim;
std::string token;
udpMsg(){};
udpMsg(const std_msgs::String::ConstPtr& msg)
{
udp = std::string(msg->data);
posOfDelim = udp.find(delimiter);
token = udp.substr(0, posOfDelim);
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
A_pub = n.advertise<udpubsub::msgA>("Amsg", 1000);
B_pub = n.advertise<udpubsub::msgB>("Bmsg", 1000);
udpMsg mainMsg;
ros::Subscriber sub = n.subscribe("chatter", 1000,mainMsg.udpMsg)
ros::spin();
No, you cannot pass a constructor as a function. udpMsg
is the name of the class itself, not the name of a function.
You can write a separate "factory function" that creates objects, and pass that to subscribe
, like
udpMsg create_message()
{ return udpMsg(); }