0
$\begingroup$

Rosanswers logo

Hi,

When using the rclcpp::Node::create_wall_timer function with std::bind , I am getting the following error

no matching function for call to ‘composition::Talker::create_wall_timer(std::chrono::seconds, std::_Bind_helper<false, void (composition::Talker::)(int), composition::Talker, int>::type)’

The error can be reproducer if one changes std::bind function inside the create_wall_timer () to add extra arguments to the function pointer.

I am attaching the code for your kind reference.

#include "composition/talker_component.hpp"
#include <chrono>
#include <iostream>
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"

using namespace std::chrono_literals;

namespace composition
{

Talker::Talker()
: Node("talker"), count_(0)
{
  // Create a publisher of "std_mgs/String" messages on the "chatter" topic.
  pubbase_= this->create_publisher<std_msgs::msg::String>("chatter");

  pub_ = create_publisher<std_msgs::msg::String>("chatter");
  timer_ = create_wall_timer(1s, std::bind(&Talker::on_timer, this,95));
 
}

void Talker::on_timer(int level)
{
  auto msg = std::make_shared<std_msgs::msg::String>();

  msg->data = "Hello World: " + std::to_string(++count_);

  // Put the message into a queue to be processed by the middleware.
 
  // This call is non-blocking.
  pub_->publish(msg);
}

Originally posted by Skyking on ROS Answers with karma: 151 on 2018-04-16

Post score: 0


Original comments

Comment by mgruhler on 2018-04-16:
@Skyking please don't use the backtick formatting for code blocks. This is for single code Statements.

Just Highlight the copy-pasted code block and click the preformatted text button (the one with 10101010) on it. Thanks.

Comment by Skyking on 2018-04-17:
Thanks @mig

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

There are some limitations on automatic type deduction of custom callbacks that can be worked around by explicitly declaring the std::function, e.g.:

  std::function<void()> callback_func = std::bind(&Talker::on_timer, this, 95);
  timer_ = create_wall_timer(1s, callback_func);

Or if you want to pass a variable in you might use std::cref in place of the 95.


Originally posted by dhood with karma: 621 on 2018-04-18

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.