Ask Your Question

Error while compiling a program to publish a string in a custom topic.

asked 2014-09-06 07:41:13 -0500

lffox gravatar image

updated 2014-09-06 08:00:19 -0500

I'm trying to publish a string under the topic, name. But I'm getting compile error and I can't figure out where exactly the problem lies also. Could someone help me out. This is my pubname.cpp file.

#include <ros/ros.h>
#include <std_msgs/String.h>

int main(int argc, char **argv)
ros::init(argc,argv,"pubname"); //node name is pubname
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::String>("name",1000); //topic is name

ros::Rate rate(2);

std_msgs::String msg("Patrick");
ROS_INFO_STREAM("Sending Name ");

This is my CMakeLists.txt,

cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED COMPONENTS roscpp std_msgs)
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(pubname pubname.cpp)
target_link_libraries(pubname ${catkin_LIBRARIES})


Scanning dependencies of target pubname
[100%] Building CXX object assignment2/CMakeFiles/pubname.dir/pubname.cpp.o
/home/Patrick/ROSthings/Auro2/src/assignment2/pubname.cpp: In function ‘int main(int, char**)’:
/home/Patrick/ROSthings/Auro2/src/assignment2/pubname.cpp:16:39: error: no matching function for call to ‘std_msgs::String_<std::allocator<void> >::String_(const char [16])’
 std_msgs::String msg("Patrick");
/home/Patrick/ROSthings/Auro2/src/assignment2/pubname.cpp:16:39: note: candidates are:
In file included from /home/Patrick/ROSthings/Auro2/src/assignment2/pubname.cpp:2:0:
/opt/ros/indigo/include/std_msgs/String.h:62:3: note: std_msgs::String_<ContainerAllocator>::String_(const ContainerAllocator&) [with ContainerAllocator = std::allocator<void>]
   String_(const ContainerAllocator& _alloc)
/opt/ros/indigo/include/std_msgs/String.h:62:3: note:   no known conversion for argument 1 from ‘const char [16]’ to ‘const std::allocator<void>&’
/opt/ros/indigo/include/std_msgs/String.h:59:3: note: std_msgs::String_<ContainerAllocator>::String_() [with ContainerAllocator = std::allocator<void>]
/opt/ros/indigo/include/std_msgs/String.h:59:3: note:   candidate expects 0 arguments, 1 provided
/opt/ros/indigo/include/std_msgs/String.h:55:8: note: std_msgs::String_<std::allocator<void> >::String_(const std_msgs::String_<std::allocator<void> >&)
 struct String_
/opt/ros/indigo/include/std_msgs/String.h:55:8: note:   no known conversion for argument 1 from ‘const char [16]’ to ‘const std_msgs::String_<std::allocator<void> >&’
make[2]: *** [assignment2/CMakeFiles/pubname.dir/pubname.cpp.o] Error 1
make[1]: *** [assignment2/CMakeFiles/pubname.dir/all] Error 2
make: *** [all] Error 2
Invoking "make" failed
edit retag flag offensive close merge delete


please post error message

bvbdort gravatar image bvbdort  ( 2014-09-06 07:53:22 -0500 )edit

hello, edited.

lffox gravatar image lffox  ( 2014-09-06 08:01:35 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2014-09-06 07:57:30 -0500

bvbdort gravatar image

updated 2014-09-06 08:38:38 -0500

Add dependencies in CmakeLists.txt

find_package(catkin REQUIRED COMPONENTS

and in package.xml



Edit: replace string message initialization of

std_msgs::String msg("Patrick");


std_msgs::String msg;
std::stringstream ss;
ss << "Patrick " << count; = ss.str();
edit flag offensive delete link more


I have added those dependencies in CMakeLists.txt. Haven't edited the xml file though, can it result in compile errors?

lffox gravatar image lffox  ( 2014-09-06 08:03:56 -0500 )edit

I tried editing the xml file also, but still same error.

lffox gravatar image lffox  ( 2014-09-06 08:18:48 -0500 )edit

check the updated answer.

bvbdort gravatar image bvbdort  ( 2014-09-06 08:38:57 -0500 )edit

Hello, it worked. Can you tell me why we have it to do this way? I saw the same in the ROSwiki also. Thank you

lffox gravatar image lffox  ( 2014-09-06 12:37:15 -0500 )edit

std_msgs::String is not same as c++ std::string, its ROS message which has string type variable data

bvbdort gravatar image bvbdort  ( 2014-09-06 13:04:40 -0500 )edit

It can also be done this way:

std_msgs::String msg; = "hello";
kparikh gravatar image kparikh  ( 2017-04-12 23:43:04 -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-09-06 07:41:13 -0500

Seen: 2,336 times

Last updated: Sep 06 '14