Reputation: 949
I have a working c++ class that has a header file and a cpp file where the following lines are in one of the functions of this class in the cpp file:
the robot(robot_ip);
services
.advertiseService<franka_control::SetJointImpedance>(node_handle, "set_joint_impedance",
[&robot](auto&& req, auto&& res) { return franka_control::setJointImpedance(robot, req, res); });
I have now moved the first line above, franka::Robot robot{robot_ip};
, to the header file of this class instead because I want to be able to reference robot
also from other functions of this class. However, now I get the error:
error: capture of non-variable ‘CLass::robot’
[&robot](auto&& req) { return sometype(robot, req, res); });
^~~~~
Upvotes: 1
Views: 7571
Reputation: 13731
You can only capture local variables into a lambda function, because otherwise these variables will not be available to this function - you can consider this function to be a stand-alone function with extra data.
But after your change robot
became a global variable, it is available inside the lambda function just like it's available to any other function - you don't need to capture it to use it (and it's not just that you don't need to - you're not allowed).
Upvotes: 3