Ask Your Question
0

Using if statements with std_msgs::String::ConstPtr

asked 2013-10-11 18:16:06 -0500

Alkaros gravatar image

updated 2014-04-20 14:09:24 -0500

ngrennan gravatar image

I have a node that is getting action calls from another node. Once it gets the action, I want to call a specific function but the if statement doesn't ever return true.

How can I get an if statement to work with a std_msgs string?

void chattersCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: %s", msg->data.c_str());
  if ((msg->data.c_str()) == "Listen") {sendStopAction();}
}

The terminal shows

[ INFO] [1381550860.740367371]: I heard: Listen

But the If statement does not return true. There is another print statement in the sendStopAction() function

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2013-10-11 19:31:33 -0500

Dirk Thomas gravatar image

I guess your code example should be:

if (msg->data.c_str() == "Listen") ...

In C++ you can not just compare two char*. You neither have to use strcmp() or use the comparison operator of std::string:

if (msg->data == "Listen") ...
edit flag offensive delete link more

Comments

Thank you. Yes, my code was msg->data.c_str(). I got confused and tried dropping the c_ just to see if it magically changed anything. Thanks again :)

Alkaros gravatar imageAlkaros ( 2013-10-11 21:54:53 -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

Stats

Asked: 2013-10-11 18:16:06 -0500

Seen: 3,061 times

Last updated: Oct 11 '13