Topics Quiz control flow statements

I am attempting the topics_quiz right now and I need to compare the output of the ranges array to some constant (1 in this case) to publish the correct Twist information to the /cmd_vel node.

I have this code to compare specific values within range to the constant:

if (msg->ranges[360] < 1.0) { var.angular.z = 0.5; } else if (msg->ranges[0] < 1.0) { var.angular.z = 0.5; } else if (msg->ranges[719] < 1.0) { var.angular.z = -0.5; } else { var.linear.x = 0.6; var.angular.z = 0; }

Where var is the Twist variable that stores the information to be published to the /cmd_vel node.

I get many error message when building this:
/home/user/catkin_ws/src/topics_quiz/src/topics_quiz.cpp:19:47: error: conversion from 'const value_type' {aka 'const float'} to non-scalar type 'std_msgs::Float32' {aka 'std_msgs::Float32_<std::allocator<void> >'} requested 19 | std_msgs::Float32 right_dist = msg->ranges[0];

Anyone have any idea what I can do to make this control flow statement work?

You are having issues with typecasting in C++. Are you familiar with C++? If not, I’d suggest you take our free course C++ for Robotics or do some research on typecasting.

If you are a beginning in ROS or programming, I would suggest you try taking the Python version of the course (and the Python 3 for Robotics course, if you need some intro to Python)