Segmentation Fault (Core Dumped) - C++ for robotics - "Classes" chapter 5

Hi @staff I am lerning C++ for robotics.
My code in c++ was compiled through catkin_make. Ir refers to “unit6_exercise” of the subjects “Classes in c++”. I had a lot of work to put this code to work regarding to create a constructor with a float parameter that I called “dist”. My intention is to create different objects for it giving them different minimum distances (“dist”) for the robot start the wall avoidance move while it approachs to the wall.
In fact I would like to use this variable created in the constructor and initialized after calling the object also into the avoiding_wall function as an argument to be used in the avoiding_wall program (block of code below it). To fix the errors of compiling the code I needed to add the “dist” variable also outside the constructor as global variable…I am new in C++ but would like to understand if what I have done is possible and how could I fix this issue of: “Segmentation Fault (Core Dumped)”. Also what is the meaning of this error? My code is below:

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

using namespace std;

class Move_Robot {
        Move_Robot(float d){ 
            float dist = d;
        float dist;
        RosbotClass rosbot;
        void avoid_wall();
void Move_Robot::avoid_wall(){
    //RosbotClass rosbot;
    float left_laser = rosbot.get_laser(685);
    float right_laser = rosbot.get_laser(35);
    float front_laser = rosbot.get_laser(0);
    float distance = dist*2;

    while (front_laser > distance) {
        if (right_laser > left_laser){
            rosbot.turn("counterclockwise", 1.0);
        } else if (right_laser > left_laser){
            rosbot.turn("clockwise", 1.0);
        } else { //if they have equal values
int main(int argc, char **argv) {
    ros::init(argc, argv, "Rosbot_move_node");

    Move_Robot my_object(1.2);
    //Move_Robot::avoid_wall(rosbot, 1.2) // calling the function of the class without arguments
    return 0;

I used a logic different of the solution, however I would like to see how it would work. So, please I will appreciate if you help me to fix this code with me to make it work and say me where is the problem that I could correct.
Thanks in advance.

Hi @marcusvini178 ,

one way of debugging C++ is using gdb, but I have to tell you that I myself I’m used to “debug” using “prints, cout”.

A possible problem I see in your code is that in the constructor you are assigning “d” to “float dist = d;” , which is a temporary variable that only exists in the constructor. I think what you want to do is: “this.dist = d;

On the avoid_wall method, you have a “while” block but you are never changing the front_laser or variable_distance. If they are never changed inside the while block itself, the loop will never finish. Your Segmentation fault problem probably lays here.

Try printing the current values of front_laser, distance, and put a counter there to better identify where the problem happens.

Knowing where the error happens is the first step to have it solved.