1

I'm trying to subscribe to different topics in ROS (one for every vehicle that pops up) using the same callback for all of them. The idea is that boost::bind will pass the topic name as an additional argument so I know which vehicle I should access in the callback.

The problem is that, even though I've gone through multiple questions on the topic, none of the solutions seem to work.

Basically, I have the following class VOBase containing a std::map<std::string, VOAgent*> agents_ with a member function as follows:

void VOBase::callback_agentState(const custom_msgs::VState::ConstPtr& vStateMsg,
        std::string topic) {
    // [...] regex to find agent name from topic string
    std::string agent_name = match.str();
    // [...] Check if agent name exists, else throw exception

    // Process message
    agents_[agent_name]->pos_ = vStateMsg->pose.position;  // etc.
}

Which I'm calling through this subscription:

void VOBase::callback_agentList(const custom_msgs::VehicleList& vehListMsg) {
    // [...] New agent/vehicle found: process vehListMsg and get agent_name string

    // Subscribe to VState
    topic_name = agent_name + "/state_estimate";
    subscribers_[topic_name] = nodeHandlePtr->subscribe<custom_msgs::VState>(topic_name, 1,
        std::bind(&VOBase::callback_agentState, this, _1, topic_name));
}

However, I'm getting a template argument deduction/substitution failed with all the candidates and this error:

mismatched types ‘std::reference_wrapper<_Tp>’ and ‘VO::VOBase*’
                    typename add_cv<_Functor>::type&>::type>()(

I've tested a number of the solutions out there, e.g. using std::ref(this) to get a std::reference_wrapper<_Tp> instead of a VO::VOBase* (the reference doesn't survive though: use of deleted function), using boost::bind instead of std::bind (but it should be all the same since C++11), with and without the ...::ConstPtr for the ROS message in the callback function arguments (and in the subscribe<acl_msgs::ViconState::ConstPtr>, etc. So I'm just juggling with partial solutions and their permutations here...

Any clues?

Pronex
  • 274
  • 3
  • 14

1 Answers1

0

I haven't looked into the specifics of the code you show (it's hard to figure out the information not shown and deduce what's required).

However, last time I helped someone with ROS subscriptions and the type-deduction, it was apparent that the handler should take a shared_ptr to the message. This might help you get started seeing a solution:

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Hmm [the docs](http://docs.ros.org/indigo/api/roscpp/html/classros_1_1NodeHandle.html#a317fe4c05919e0bf3fb5162ccb2f7c28) also reveal non-shared-ptr overloads. I'll try to have a look later. Meanwhile, do read that other answer, it might give you a clue. – sehe Aug 27 '17 at 16:00
  • Thanks, I had tried using the `shared_ptr`, but for some reason, it hadn't worked out before. Now I rewrote the whole thing and it worked! – Pronex Aug 28 '17 at 14:04