ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

got Problem in pub,sub and dynamics Tutorial

asked 2020-11-19 19:26:32 -0500

Leezy gravatar image

in git node_example/src/talker.cpp

i dont know where comes ExampleTalker instead node_example::talker

and

create ExampleTalker::ExampleTalker(Handler) -> but use node_example::ExampleTalker node(nh) in node_example/src/nodes/talker_node.cpp

https://github.com/tdenewiler/node_ex...

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-11-23 04:18:17 -0500

Weasfas gravatar image

updated 2020-11-23 04:26:38 -0500

Hi @Leezy,

I recommend you to start learning some concepts of C++ and, in general, of Object-oriented programming. You have plenty of resources on the Internet to learn about conpects like class, like this or this. Although this is a bit out of the scope of this forum but I will try to answer your questions.

The code you are using contains two chunks of code: the header file .h that declares the class which is going to use the publisher, and the .cpp code that is the actual implementation of the class. To be able to use the class you need a special function that is going to initialize the class to work. In your code, the ExampleTalker is the class that implements the talker (node with publisher) and the line ExampleTalker::ExampleTalker(nh) is the line where this function is implemented, that is called a constructor of the class and it is used to initialize all class attributes (including nodehandle and publisher and reading from the parameter server). That is the syntax used in C++ to define a constructor and it is something particular to the class constructor implementation; its name is repeated because the contructor must be inside its actual class (whose name is ExampleTalker).

However, to answer your second question. When you want to use the class, you have to initialize it by using the constructor already implemented. Since the code is inside a name_space (node_example) and the constructor name is ExampleTalker you need to initialize you new class like node_example::ExampleTalker node(nh) that is: name_space::class:_name(params). Now, with that line you already have you talker object ready to publish new msgs.

Hope that help.

Regards.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-11-19 19:22:49 -0500

Seen: 58 times

Last updated: Nov 23 '20