First, define your struct like this in C++:
struct form { // note: Qt naming convention would use upper case first letter
QPolygon polygon;
QRect position;
};
At the end of this answer there is a more modern alternative, but first, to directly fix your 20 years old style C++, write your code like this (you shouldn't, but just so you know how to do it):
For this, lets assume you have
form *forms;
Then you could make your code not crash, if you wrote it like this:
int n = 10
forms = new form[n]; // bad style in modern C++, use std::vector instead
while (n > 0) {
--n; // indexes go 9..0
//forms[n].position.setRect(0,0,0,0); // constructor did this
//forms[n].polygon=QPolygon(0); // constructor did this
forms[n].polygon = QPolygon(QRect(x, y, w, h));
}
Your error might have been, because your QPolygon
and QRect
instances inside form
structs were not properly constructed. Hard to say, what you did was undefined behavior, accessing uninitialized memory like that. Additionally, you had n==10
in your loop's first iteration, which is outside valid index range 0..9, that might have crashed too.
Additionally, when you allocate an array with new
, you must also delete it as array, so that array elements get properly destructed:
delete[] forms;
With modern C++, you wouldn't need to do this, you'd use value types or smart pointers.
Finally, a more modern version, which is just all around easier and safer, and you don't need to worry about releasing memory, etc:
std::vector<form> forms;
int n = 10
forms = std::vector<form>(n); // assign a vector of length n to forms
while (n > 0) {
... contents of the loop are same as above ...
}