gz-math icon indicating copy to clipboard operation
gz-math copied to clipboard

non-const `Color::operator[]` should return mutable reference

Open azeey opened this issue 1 year ago • 3 comments

Environment

  • OS Version: Ubuntu 22.04
  • Source or binary build? main, e0193a59072cc4a083dedaecf05957d059997b02

Description

  • Expected behavior: To be able to mutate a component of Color using operator[]
  • 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

azeey avatar Feb 05 '24 23:02 azeey

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

Avisheet avatar Feb 21 '24 18:02 Avisheet

Hi, is this issue still open? If it hasn't been implemented I would be happy to open the pull request.

aagrawal05 avatar Apr 04 '24 14:04 aagrawal05

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 .

Avisheet avatar Apr 04 '24 14:04 Avisheet