Ask Your Question

How to use topic message type as an argument of a function?

asked 2021-11-21 06:55:56 -0500

Kevin1719 gravatar image

In the subscriber callback function, I would like to use topic message type as an argument so that I can use for any topic:

void callback(message_type &msg)
ros::Subscriber sub = nh.subscribe(topic_name, 1000, callback);

Any suggestion?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-11-21 09:17:24 -0500

Mike Scheutzow gravatar image

I'm 99% certain that this can not be done. In c++, ros1 messages do not inherit from a common base class, so one message's type is unrelated to all other message types (in a class hierarchy sense.) By definition, a topic can only have one message type. To help avoid bugs, the ros developers decided to make you declare exactly what message type you think is arriving on the topic.

edit flag offensive delete link more



It can be done, but I've never tried it. It can also be done with templates, I would guess, but perhaps if the OP described in more detail what they wanted to do...

KenYN gravatar image KenYN  ( 2021-11-22 02:03:31 -0500 )edit

@KenYN Sometimes it's best to recognize that the cure is worse than the disease. But thanks for the link - that is an approach I haven't seen before.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-11-22 13:57:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2021-11-21 06:55:56 -0500

Seen: 49 times

Last updated: Nov 21 '21