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

Revision history [back]

Actually, considering the error nature: error: invalid initialization of non-const reference of type ‘bool&’ from an rvalue of type ‘bool’, the figure below illustrates that:

linker error

The correct way to solve this bug is passing this rvalue (bool_test.data) by const to reference, once the compiler denies the reference passing of an rvalue (temporary and literal types) without being const because it does not create a non-const reference to rvalues preventing operations like "3 = 5" and other stuff. The right way to do that is:

#include <ros/ros.h>
#include <std_msgs/Bool.h>
#include <stdint.h>

void test2(const uint8_t &test)
{    
    return;
}
void test(const std_msgs::Bool &bool_test)
{
    test2(bool_test.data); //Not okay before, But OKAY now hahahaha
    return;
}

int main(int argc, char** argv)
{
    std_msgs::Bool bool_obj;
    test(bool_obj);

    return 0;
}

Moreover, remember, always try to use pass by const-to-reference, once errors like that are avoided and, consequently, you avoid other variables values changing and helps the compiler to optimize the code execution.

I hope to help you! If you have some question do not hesitate to ask me.