Operator overloading threevariables without tmp variable

Hello, I currently have this code:

1
2
3
diffuse_color.red   += light_intensity.red * lambert * mat.kd.red;
diffuse_color.green +=  light_intensity.green * lambert * mat.kd.green;
diffuse_color.blue  += light_intensity.blue * lambert * mat.kd.blue;


Where light_intensity and mat.kd are structs made by three floats and lambert an ordinary float;

My intention was to replace it by something like:

diffuse_color += light_intensity * lambert * mat.kd;

For doing such I decided to start by overloading twice the * operator:

1
2
3
4
5
6
7
inline Color operator * (Color& c, float a) {
   Color color;
   color.red = c.red * a;
   color.green = c.green * a;
   color.blue = c.blue * a;
   return color;
}

1
2
3
4
5
6
inline Color operator * (Color& c, Color d) {
   Color color;
   color.red = c.red * d.red;
   color.green = c.blue * d.blue;
   color.blue = c.green * d.green;
}


I expected it to solve the matter, but the compiler tells me

/media/34GB/demos/asmfrt-master/RayTracer.cpp|299|error: no match for ‘operator*’ in ‘operator*(((Color&)(& light_intensity)), lambert) * mat.Mtl::kd’|


I guess I can solve this issue using a temporary Color structure, that will store the result of a float by color multiplication (or a color by color) and then to do the other multiplication. And of course, there is the += operator to be overloaded

But I would like to do everything in a single line of code. Is it possible? Thanks for help.
Last edited on
Try making the Color& param to those two functions const.
And don't forget the return for the second one!
Your argument color should be const:

Color operator * (const Color& c, float a) 
Color operator * (const Color& c, const Color& d)

BTW, inline is fairly meaningless these days... let the compiler do its magic on its own.
Last edited on
many compilers support some form of force_inline if you want to override the compiler's decision and time it to see if you can make it better. Straight inline keyword is often ignored entirely, though. You can also #include to inline if you must on a compiler that lacks a force, but its ugly. Most of the time, all you are going to do is discover that the compiler made the right choice. Once in a great while, you will outsmart it.
Ok, I am able to compile it and run it (after adding a "return color" and swapping the blue and green colors that were added in the second overloading). Anyway, as I mentioned in the first message, my final goal was to replace

1
2
3
diffuse_color.red   += light_intensity.red * lambert * mat.kd.red;
diffuse_color.green +=  light_intensity.green * lambert * mat.kd.green;
diffuse_color.blue  += light_intensity.blue * lambert * mat.kd.blue;


by this:

diffuse_color += light_intensity * lambert * mat.kd;

So I still had to overload += operator.

I tried to do so with
1
2
3
4
5
6
7
inline Color operator += (const Color& c, const Color& d) {
   Color color;
   color.red = c.red + d.red;
   color.green = c.green + d.green;
   color.blue = c.blue + d.blue;
   return color;
}


However this is not producing correct results. I am fairly sure the problem is in this last overloading, because if I do

1
2
3
4
5
6
7
8
9
diffuse_color.red   += /*ALBEDO**/  light_intensity.red * lambert * mat.kd.red;
                  diffuse_color.green += /*ALBEDO**/ light_intensity.green * lambert * mat.kd.green;
                  diffuse_color.blue  += /*ALBEDO**/  light_intensity.blue * lambert * mat.kd.blue;
                  cout << "diffuse_color: " << diffuse_color.red << ' ' << diffuse_color.green << ' ' << diffuse_color.blue << endl;

                  final_color = light_intensity * lambert * mat.kd;
                  cout << "final_color: " << final_color.red << ' ' << final_color.green << ' ' << final_color.blue << endl;
                  cout << "" << endl;
                  exit (-1);


The printed values are the same. However, if I remove the exit (-1), the values start to differ.

So the problem is likely to the in the overloading of +=. I tried this

1
2
3
4
5
6
inline Color Color::operator += (const Color& c) {
   this.red = this.red + c.red;
   this.green = this.green + c.green;
   this.blue = this.blue + c.blue;
   return this;
}


but I keep receiving
/media/34GB/demos/asmfrt-master/GeometricObjects.h|42|error: no ‘Color Color::operator+=(const Color&)’ member function declared in class ‘Color’|


So what shall I try now?
Topic archived. No new replies allowed.