Enumerated Array

Hi All,

I have been given the following information about a "colours" type.
in a header file.

/*
* An enumeration to correspond to the colours needed
*/
enum Colours {
BLACK = '0', RED, GREEN , YELLOW,
BLUE, MAGENTA, CYAN, WHITE
};

/*
* definition of a 2D array of Colours type
*/
const int HLINES = 20;
const int VLINES = 20;
typedef Colours Colour2DArray [HLINES][VLINES];


I am trying to set a generic Colour2DArray constructor to have the initial value of black using a for loop.

heres what I have done.


Image::Image()
{


// Set all elements of theARray to BLACK
for (int nCol=0; nCol<20; nCol++)
for (int nRow=0; nRow<20; nRow++)
theArray[nRow][nCol] = BLACK;
}


this won't work at the moment

.
I was wondering if This Array

const int HLINES = 20;
const int VLINES = 20;
typedef Colours Colour2DArray [HLINES][VLINES];


Expects to receive values of colours type or int type;
As the [][] is constant ints to show its size but what does it accept to fill the array.


I have created a class Image,


class Image
{


public:
Image();

double& operator()(const int nCol, const int nRow);
void operator()();


private:
Colour2DArray theArray[HLINES][VLINES];
int nCol;
int nRow;

};


But get the following error when compiling the constructor.


image.h: In constructor âImage::Image()â:
image.h:42: error: incompatible types in assignment of âColoursâ to âColours [20][20]â



Thanks for any ideas
Brendan









Type Colours (an enum) is not compatible with type Colour2DArray (a 2D array of enums). Think about it a bit and/or try compiling with the -Wall option. Hint: there is something wrong with your definition of theArray's type.

Unrelated, but just as important, is your for loops in the constructor. Why are you using 20 instead of HLINES and VLINES?

Hope this helps.
Last edited on
Hi Thanks for your reply
I'm still a bit Hazy on this array.
When you said that there is something wrong with the arrays definition
Should it be just

Colours theArray[VLINES][HLINES};

and if so how do I fill it with the enumerated values,

Thankyou,
Brendan
You've got the idea.

Now, you've got a type named "Colour2DArray" (which is a type just like any other: int, char, float, etc).

Since the type of Colour2DArray is an array of VLINES by HLINES Colours, you can use it to declare those arrays. So instead of

Colours theArray[VLINES][HLINES];

you can just write

Colour2DArray theArray;

Either definition declares "theArray" as an array of VLINES by HLINES Colours. I hope that makes sense.

To fill your array, do just like you are doing. Except, how do you know that VLINES is always going to be equal to 20? What if, a month from now, your professor asks you to modify the assignment to have a larger array?

You'll open it up and change VLINES,HLINES to something else (say, 100,100). Everything works great except your constructor only sets the first 20 by 20 colors to black and leaves everything else some random value...

for (int nCol=0; nCol<HLINES; nCol++)

Hope this helps.
Topic archived. No new replies allowed.