Ask Your Question

Problem with subscribe and callback function

asked 2014-08-22 12:44:51 -0500

Andromeda gravatar image

updated 2014-08-22 13:05:36 -0500

Hi everybody, since my code is really long I will try to keep it minimal

in my program I wrote a class which defines 2 storing variables:

geometry_msgs::PoseWithCovarianceStamped initial_pose_;
geometry_msgs::PoseWithCovarianceStamped pose;

and subscribes to the /initialpose topic:

ros::topic::waitForMessage<geometry_msgs::PoseWithCovarianceStamped>( "initialpose", ros::Duration( 60.0 ) );
this->pose_sub_ = nh_.subscribe( "initialpose", 10, updateInitialPose );

the callback function has been defined as:

void updateInitialPose( const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& pose )
initial_pose_ = pose;

but compiling I get all the time the following error:

/home/wilhem/workspace_ros/src/turtlebot_guide/src/main.cpp: In member function ‘void robotMovement::updateInitialPose(const ConstPtr&)’: /home/wilhem/workspace_ros/src/turtlebot_guide/src/main.cpp:45:17: error: no match for ‘operator=’ (operand types are ‘geometry_msgs::PoseWithCovarianceStamped’ and ‘const ConstPtr {aka const boost::shared_ptr<const geometry_msgs::posewithcovariancestamped_<std::allocator<void="">

}’) initial_pose_ = pose; ^ /home/wilhem/workspace_ros/src/turtlebot_guide/src/main.cpp:45:17: note: candidate is: In file included from /home/wilhem/workspace_ros/src/turtlebot_guide/src/main.cpp:10:0: /opt/ros/indigo/include/geometry_msgs/PoseWithCovarianceStamped.h:57:8: note: geometry_msgs::PoseWithCovarianceStamped_<std::allocator<void> & geometry_msgs::PoseWithCovarianceStamped_<std::allocator<void> ::operator=(const geometry_msgs::PoseWithCovarianceStamped_<std::allocator<void> &) struct PoseWithCovarianceStamped_ ^

Even the candidated definitions are to me not really clear. I have already searched through the API and class definitions but I couldnt find a solution. Any help? Thanks in advance

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted

answered 2014-08-22 13:06:29 -0500

Murilo F. M. gravatar image

You are trying to assign a shared pointer geometry_msgs::PoseWithCovarianceConstPtr to an instance of geometry_msgs::PoseWithCovarianceStamped.

The operator= is not overloaded to perform deep copies (AFAIK).

The shared pointer cannot know the structure of the underlying object. A slightly annoying, but easy way of working around that is by copying the data field by field, such as:

initial_pose_.header = pose->header;
initial_pose_.pose = pose->pose;

Also note that your global variable named pose has the same name as the shared pointer (message) received in the callback.

Perhaps you could avoid using global variables in the process of making the necessary changes! :-)

edit flag offensive delete link more



initial_pose_ = *pose should also work.

ahendrix gravatar image ahendrix  ( 2014-08-22 13:09:24 -0500 )edit

It works!!!!!!!!!!!! Thanxxxxxxxxx!!!! But another question, how should I know that the operator = is not overloaded!?!??!?!!???

Andromeda gravatar image Andromeda  ( 2014-08-22 13:19:46 -0500 )edit

From the error you got, it said: error: no match for ‘operator=’ and showed the object types involved.

Murilo F. M. gravatar image Murilo F. M.  ( 2014-08-22 13:25:38 -0500 )edit

Thanx Murilo, could you me explain please what is boost::shared_ptr ?

Andromeda gravatar image Andromeda  ( 2014-08-22 14:02:47 -0500 )edit

I think it is a hard topic to explain in this 'comments area'. Besides that, it falls out of the scope of the original question (we must keep the site tidy). A nice tutorial I read when I first learnt about smart pointers was this one.

Murilo F. M. gravatar image Murilo F. M.  ( 2014-08-22 14:30:45 -0500 )edit

answered 2014-08-22 12:58:06 -0500

ahendrix gravatar image

You callback should be:

void updateInitialPose( const geometry_msgs::PoseWithCovarianceStamped::ConstPtr& pose )
edit flag offensive delete link more


I have updated the post: same problem :( :(

Andromeda gravatar image Andromeda  ( 2014-08-22 13:06:05 -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

1 follower


Asked: 2014-08-22 12:44:51 -0500

Seen: 5,042 times

Last updated: Aug 22 '14