gz-math
gz-math copied to clipboard
non-const `Color::operator[]` should return mutable reference
Environment
- OS Version: Ubuntu 22.04
- Source or binary build?
main
, e0193a59072cc4a083dedaecf05957d059997b02
Description
- Expected behavior: To be able to mutate a component of
Color
usingoperator[]
- Actual behavior: Since it returns a value, any mutation is not applied to the component in
Color
https://github.com/gazebosim/gz-math/blob/e0193a59072cc4a083dedaecf05957d059997b02/include/gz/math/Color.hh#L162
The issue you're describing is related to the operator[] overload for the Color class. The problem is that the operator is declared to return a float instead of a reference to a float. As a result, when you use the operator to access a component of the color and attempt to mutate it, the mutation doesn't apply to the actual color instance.
To fix this issue, you should modify the declaration of the operator[] to return a reference to a float. Here's the corrected declaration:
public: float& operator[](const unsigned int _index);
This modification allows you to use the operator to both access and mutate the individual components of the Color class.
I hope by these changes in the code you will be able to mutate a component of color using operator
Hi, is this issue still open? If it hasn't been implemented I would be happy to open the pull request.
Hi @aagrawal05 , I am currently working on this issue. I'm getting some run time errors while running test cases. You can check those details here. It would be appreciated if you can add some value to that .