ssloy/tinyrenderer

TGAColor expects an array and not integer arguments

tsadarsh opened this issue · 5 comments

Compiling my mesh.cpp throws the following error in at this line:

const TGAColor white = TGAColor(255, 255, 255, 255);
 error: no matching function for call to ‘TGAColor::TGAColor(int, int, int, int)’
    6 | const TGAColor white = TGAColor(255, 255, 255, 255);
tgaimage.h:23:8: note: candidate: ‘constexpr TGAColor::TGAColor()’
   23 | struct TGAColor {
      |        ^~~~~~~~
tgaimage.h:23:8: note:   candidate expects 0 arguments, 4 provided
tgaimage.h:23:8: note: candidate: ‘constexpr TGAColor::TGAColor(const TGAColor&)’
tgaimage.h:23:8: note:   candidate expects 1 argument, 4 provided
tgaimage.h:23:8: note: candidate: ‘constexpr TGAColor::TGAColor(TGAColor&&)’
tgaimage.h:23:8: note:   candidate expects 1 argument, 4 provided

Going through the definition of TGAColor in tgaimage.h it is very clear that it expects an array:

struct TGAColor {
    std::uint8_t bgra[4] = {0,0,0,0};
    std::uint8_t bytespp = 4;
    std::uint8_t& operator[](const int i) { return bgra[i]; }
};

So shouldn't TGAColor(255, 255, 255, 255); be changed to TGAColor({255, 255, 255, 255}); in the Wiki pages too?

Doing so compiles the program successfully without any errors. Is this an actual bug or am I not understanding something?

Hello @tsadarsh
The definition of the struct TGAColor looks different in the tgaimage.h I loaded up from the tinyrenderer repository. Which tgaimage.h are you using?

I am referring to the current (master) branch. Here is the file tgaimage.h.

@tsadarsh I see. So this is the problem, why the code does not work, because this is the wrong tgaimage.h file you are referring. If you look at the README.md file @ssloy writes:

... The entire code is available on github, and [here](https://github.com/ssloy/tinyrenderer/tree/909fe20934ba5334144d2c748805690a1fa4c89f) you will find the source code I give to my students.

So no, I do not think that the Wiki needs any changes. This just in case you meant the README.md file.

And yes, you are right with the code change. Just a detail: I think you should use curly brackets to init a struct, like this:

const TGAColor white = TGAColor{{255, 255, 255, 255}};

It would make sense to add the corresponding constructor to the TGAColor class. Will once I have access to a computer

Thank you @digitsensitive for the clarification.