PDA

View Full Version : QT enum array



angarali06
3rd April 2011, 08:53
Hi everybody,

How can I declare an array of shape in QPainter?

In the header file, I have an enumerated shape. It works fine without making array. But now I want to have an array of size 10 of this enumerated shape. I could not successfully convert the shape to an array of size 10. Please help..

Here are my code pieces:

my header file


enum Shape {Kare, Hexa, Rect};
void setShape(Shape shape[10]);
Shape shape[10];

my cpp file


void RenderArea1::setShape(Shape shape[10])
{
for (int kat = 0; kat < 10; kat += 1) {
this->shape[kat] = shape[kat];
update();
}
}

for (int kat = 0; kat < 10; kat += 1) {

switch (shape[kat]) {

case Kare:
....
case Hexa:
....
case Rect:
....
}
}

RenderArea1::Shape shape[Window::kat] = renderArea1->Kare;
renderArea1->setShape(shape[Window::kat]);
renderArea1->update();


Thanks in advance..

wysota
3rd April 2011, 09:30
I could not successfully convert the shape to an array of size 10.
What does it mean exactly? It doesn't compile? What error do you get?

stampede
3rd April 2011, 09:36
Hard-coded stuff is bad. Imagine that at some point you'd like to use 20 shapes instead of 10 - in your case that means replacing the values in every place in your code. This is bug-prone, because you can forget about something. Simple improvement could be to use #define for number of shapes:


#define N_SHAPES 10
...
enum Shape {Kare, Hexa, Rect};
void setShape(Shape shape[N_SHAPES]);
Shape shape[N_SHAPES];
...
for (int kat = 0; kat < N_SHAPES; kat += 1) {
this->shape[kat] = shape[kat];
update();
}
//... and so on

Furthermore, I'd switch to one of Qt containers instead of C-styled arrays - for example, take a look at QList (http://doc.qt.nokia.com/latest/qlist.html).

angarali06
3rd April 2011, 09:42
thanks stampede but I still got these errors:

for line 22
expected constant expression
cannot allocate an array of constant size 0
error C2440: 'initializing' : cannot convert from 'RenderArea1::Shape' to 'RenderArea1::Shape []'
There are no conversions to array types, although there are conversions to references or pointers to arrays
for line 23
'RenderArea1::setShape' : cannot convert parameter 1 from 'RenderArea1::Shape' to 'RenderArea1::Shape []'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

stampede
3rd April 2011, 16:21
RenderArea1::Shape shape[Window::kat] = renderArea1->Kare;
What do you mean by this line ? Window::kat is some kind of enum value ? This declares an array of Shapes, with number of elements equal to Window::kat value, which seems to be 0 according to compiler message. Next you want to assign a single value for the array, you can't do that because there are different types.

renderArea1->setShape(shape[Window::kat]);
setShape method expects an array, and you are giving it only one value ( shape[Window::kat] is probably the same as shape[0], assuming that Window::kat==0 ). Probably you mean


renderArea1->setShape(shape);

but there is still first error to fix.
For me this looks like a basic C/C++ issues, not really related to Qt.