1

I have defined a structure

struct Pixel { uint8_t r, g, b; };

And I have a function

void setPixel(int x, int y, const Pixel& color);

What color value do I have to input for setPixel() function to get it working? I do not know how should I define it.

What am I missing here?

Ady96
  • 686
  • 4
  • 12
  • 35
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – UnholySheep Oct 09 '17 at 12:51
  • Try changing the data type within the pixel struct. uint8_t is a optional implementation so your compiler may not support it but this is a bit of a guess. – Kits Oct 09 '17 at 12:56
  • "What color value do I have to input for `setPixel()` function to get it working?" Any color you want? And what are you asking about, how to use the first `setColor` function which takes a `Pixel` structure as argument, or about the linker error you get? Keep it to one question per question please. – Some programmer dude Oct 09 '17 at 12:58
  • @Kits uint8 is supported. The error I am getting is because those two functions defined below the first setPixel() have possibly wrong values or something else is wrong there. First setPixel() is not used because I do not know what value should I input, since its Pixel type. – Ady96 Oct 09 '17 at 12:58
  • @Someprogrammerdude since the linker error I am getting is basically related to the first problem because it is an alternative, I think it might be okay to leave those two questions in one. But if you really think I should split them, I will. – Ady96 Oct 09 '17 at 13:04
  • You indicated that you still have a problem, after accepting an answer (mine or any other). If you consider it the same problem with another twist, please add details, observations, error messages... If you think you have solved one problem and have another one close by (very scientific thinking, my respect) please create a separate question. – Yunnosch Oct 09 '17 at 13:39

3 Answers3

2

Look at the prototype provided for the function you want to call.

void setPixel(int x, int y, const Pixel& color);

Observations:

  • it wants one parameter for x, an int
  • it wants one parameter for y, an int
  • it wants one parameter for color, a Pixel,
    it happens to be a reference parameter, but that is not immediatly relevant

So first of all, any call with more than three parameters cannot work.

Second, you need to give three parameters of the right type, the first two of type int, which can be r-values. Your x and y might be OK, if they are int, as in

int x = 5;
int y = 5;

However, this could be a bit confusing, using the same name inside and outside of the function you are going to call. So I recommend

int outsideX = 5;
int outsideY = 5;

The third parameter, being a reference parameter, requires an actual (constant) variable of type Pixel. You need to set one up.

Pixel outsideColor;
outsideColor.r = 255;
outsideColor.g = 0;
outsideColor.b = 255;
// this will be bright magenta

Having done these preparations you can call the function as

setPixel(outsideX, outsideY, outsideColor);
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • This explains the problem for third parameter, but the error still persists. I assume it is something else, but I dont even know what might be wrong, or where to look. – Ady96 Oct 09 '17 at 13:28
0

You just have to construct Pixel correctly, for example, you could use aggregate init or manual construction:

struct Pixel { uint8_t r, g, b; };

void setPixel(int x, int y, const Pixel& color) {
  std::cout << "Setting " << x << " " << y << " to: " << (unsigned)color.r << " " << (unsigned)color.g << " " << (unsigned)color.b << std::endl;
}

int main() {
  // Using aggregate init
  setPixel(1, 1, { 255, 255, 255 });

  // Manually
  Pixel pix;
  pix.r = 255;
  pix.g = 255;
  pix.b = 255;
  setPixel(1, 1, pix);
}
Daniel Trugman
  • 8,186
  • 20
  • 41
0

Well apparently there's no function which takes the color as three distinct arguments.

Instead you have to use the function taking the structure as argument. Which is easy, as in

setPixel(some_x, some_y, { some_r, some_g, some_b });

Where the variables are defined and initialized to some values, or values direct.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621