1

I'm running into a basic array issue that I can't seem to get my head around.

I have a class "StaticDisplayLayer" and the constructor takes 2 parameters - an int and a pointer to an array of 3 unsigned short ints:

//constructor definition:
StaticDisplayLayer(int type, unsigned short *displayColor[3]);

//constructor code:
StaticDisplayLayer::StaticDisplayLayer(int type, unsigned short *dColor[3]) : DisplayLayer(type)
{
    displayColor = dColor;
}

I'm trying to create an instance of that class using the following:

unsigned short layerColor[3] = {(unsigned short)255,(unsigned short)255,(unsigned short)255};
StaticDisplayLayer myLayer(1, &layerColor);

My understanding is that &layerColor is a pointer to the layerColor array but the compiler is giving me the following error:

no matching function for call to `StaticDisplayLayer::StaticDisplayLayer(int, short unsigned int (*)[3])'
Candidates are:
   StaticDisplayLayer::StaticDisplayLayer(const StaticDisplayLayer&)
   StaticDisplayLayer::StaticDisplayLayer(GLenum, short unsigned int**)

I know the second candidate is the one I'm trying to use but obviously I'm not understanding the concept of pointer to an array. If someone could shed some light on how to call that constructor and/or any resources that explain this I'd appreciate it - so far my searches online haven't really turned up much.

TheOx
  • 2,208
  • 25
  • 28
  • have you considered using `std::vector` for this? You can get around all those complicated syntax with it. – Naveen Dec 14 '11 at 17:47
  • Yeah I was about to rewrite it using vector but didn't want to just let this go without understanding what I was doing wrong. – TheOx Dec 14 '11 at 17:59

3 Answers3

5

unsigned short *dColor[3] is not a pointer to an array, it's a pointer to an array of pointers. The [3] is ignored and replaced with another pointer, as it is a function parameter. In other words, it decays. To make a pointer to an array, use unsigned short (*dColor)[3], which is a pointer to array of size 3 to unsigned short.

An even better idea in C++ is to use a reference to an array:

unsigned short (&dColor)[3]

And just pass layerColor.

Xeo
  • 129,499
  • 52
  • 291
  • 397
1

&layerColor has type pointer to array of 3 unsigned shorts. Your constructor, on the other hand, expects unsigned short *[3] which is array of three pointers to unsigned short. As a matter of fact, as a function parameter type, it's a pointer to pointer to unsigned short - the dimension is ignored completely. I think what you meant was for your constructor to take just a pointer to unsigned short and pass layerColor

StaticDisplayLayer(int type, unsigned short *displayColor);

unsigned short layerColor[3] = {255,255,255};
StaticDisplayLayer myLayer(1, layerColor);

Note that in this case it is the caller's responsibility to pass an array that has at least 3 elements in it. Alternatively you could make the function take an array of exactly 3 ushorts by means of

 StaticDisplayLayer(int type, unsigned short (&displayColor)[3]);

But in this case you won't be able to pass dynamically allocated arrays.

I cannot but notice that you lack certain basic knowledge in C++, so I suggest you to read a good C++ book

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
1

unsigned short displayColor[3] is saying an array of unsigned short pointers or unsigned short * essentially.

unsigned short layerColor[3] is an array of unsigned shorts, not an array of unsigned short pointers.

I would instead make a struct or class for color and then pass a pointer or reference to one.

Alturis
  • 74
  • 8