Clarification on the content of C++ for Robotics Basics

Hi, i am familiar with using Python inROS and was learning to do this in C++, using the course C++ For Robotics. I have a couple of queries regarding the course.

Question 1:
Under Unit 1, unit1_exercise.cpp

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>
  • What is the difference between " " and <> based on usage above? from my understanding the syntax is #include <Package_name/header_file.h>, so why are we using " " ?

Question 2:
Under Unit 1, unit1_exercise.cpp

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");
  • What is **argv? is it a pointer? In the course content we talk about using pointers that use a single * to get the value at an address, but there is no mention of ** in the content? Can i know what this is intended to do?

  • I understand from the content that pointers are used in functions to avoid data duplication (like say when passing array into a function) and to make things faster. Can i know how this problem is addressed in python since i have rarely used pointer when working on ROS with python?

Question 3:
Under Unit 1, unit1_exercise.cpp

#include "rosbot_control/rosbot_class.h"
#include <ros/ros.h>

int main(int argc, char **argv) {
  ros::init(argc, argv, "rosbot_node");

  RosbotClass rosbot;
  rosbot.move();

When working with Python files, in order to use a class, I had to specifically import the class as
from python_file_name import Class_written_in_python_file

or i had to import the entire module and use dot operator as below :

import python_module
python_module.Class_written_in_python_file

Question
When we write the line #include "rosbot_control/rosbot_class.h", is all its contents readily available to use directly?

We have used RosbotClass rosbot; directly without any dot operator or anything. Also in ROS_INFO_STREAM(x_2 << " and " << y_2);, we are able to use the function ROS_INFO_STREAM directly.

At the same time, we are using scope operator in ros::init to access it. Why the two different approaches?

Question 4:

class Hobbit {
    public:
    
       Hobbit(string a,float b,int c) {
           name = a;
           height = b;
           age = c;
       } //constructor
    
       string name;
       float height; //in meters
       int age;

When creating a constructor, why are the data types (string name; float height; //in meters int age;), mentioned after the constructor is defined? Since the constructor is compiled first, how will the program know the datatype of say “name” when its called before its defined.

Question 5:

  • What the difference between scope operator and namespace? since cout can be used using namespace std as well as std:: cout.

Question 6:
In Unit 5, rosbot_class.h,

#ifndef ROSBOT_CLASS_H
#define ROSBOT_CLASS_H
#include "geometry_msgs/Twist.h"
#include "nav_msgs/Odometry.h"
#include "sensor_msgs/LaserScan.h"
#include <list>
#include <ros/ros.h>
#include <string>

What do the first two lines mean?



I am new to C++ and these clarification would really help. thanks in advance. :grinning:

Hi @Joseph1001,

Answer to your question 1 is telling preprocessor where to find the header file in need. By using <> you tell preprocessor to use the usual path whereas using “” tells preprocessor to first search the directory in which this file is stored for the required header.

Answer to question 2 is, single * stands for a pointer, double * means pointer to a pointer. This is multiple indirection concept of c++. You can read more about it here https://www.tutorialspoint.com/cplusplus/cpp_pointer_to_pointer.htm.

Concept is same for python as well.

1 Like

Thanks for replying @rupaligarewal22.
When we use the <>, you said the preprocessor takes the usual path, is there a general directory where all the <> files are stored?

With “”, you said that preprocessor searches for the file /rosbot_class.h in the package rosbot_control, does the preprocessor already know the location of the rosbot_control package?

@Joseph1001 answer to question 3 is: first understand how header files work in cpp. Header files in cpp contains a well written code which you can access just by including the header file.
Different language follows different syntax but the concept is same i.e. write code such as to be able to reuse them in times of need.

Thanks for sharing the link, it was informative. Can you tell me why we use it? We can use a single pointer also to point to an address, then why two?

@Joseph1001 refer this https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename

@Joseph1001 check this out
https://stackoverflow.com/questions/71108/to-what-use-is-multiple-indirection-in-c/71250

Not every time you have access rights directly to a data structure. Using multiple indirection makes sure you play around without outsider telling whats in the main code.

@Joseph1001 that’s the beauty of templates . Just write a generic code and on the go you can define the data type and all the functions will still be usable. Read more about template to know the background story.

Thanks @rupaligarewal22 , this was really helpful.

I will also do a couple of courses from other sites in addition to Construct to work better with C++.

I think since the content in this course is tailored mainly towards application in ROS, they did not go into depth.

Thanks for taking the time. :grinning:

1 Like

The using namespace statement just means that in the scope it is present, make all the things under the std namespace available without having to prefix std:: before each of them. Refer this article https://www.tutorialspoint.com/What-does-using-namespace-std-mean-in-Cplusplus

https://www.cprogramming.com/reference/preprocessor/ifndef.html