Shape ***newData = new Shape[_frames][_capacity]; //figure out this
Dies tut nicht, was Sie denken, dass es tut. Es weist nur ein Array von Zeigern auf Arrays im freien Speicher zu. Unter der Annahme, dass _capacity
eine Konstante ist, ist jeder Zeiger im Array ein Zeiger auf ein anderes festes Array, das genau _capacity
Instanzen von Shape
enthält. Hier ist ein Bild:
index
+-----------+
newData --> | 0 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| 1 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| 2 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
| |
.......
| |
+-----------+
| _frames-1 | (pointer to a fixed array of _capacity instances of Shape)
+-----------+
(This is the only thing that is actually allocated)
Der Ausdruck new Shape[_frames][_capacity]
nicht genügend Speicher zuweisen, um tatsächlich _frame*_capacity
Instanzen Shape
halten. Da jeder Zeiger im Array auf ein anderes Array von festen Größe zeigt, beschwert sich der Compiler über die nicht konstante _capacity
in Ihrem Code-Snippet.
Anstatt das schmerzlich verwirrend Shape***
und new[_frames][_capacity]
Geschäft zu verwenden, verwenden Sie ein std::vector
eine lineare Anordnung von Shape
s zu halten, dann ein Adressierungsschema verwenden, um Zeilen und Spalten zu emulieren, wie folgt aus:
// Exposition only
std::vector<Shape> data;
void ResizeMatrix(std::vector<Shape>& data, int frames, int capacity)
{
data.resize(frames * capacity);
}
// Populate data with Shapes
void IndexMatrix(int capacity_index, int frame_index, int capacity)
{
Shape& aShape = data[capacity_index + (capacity * frame_index)];
// Do something with it
}
Warum ‚aren Verwenden Sie 'std :: vector'? –
Warum verwenden Sie nicht einen 'std :: vector' mit der Größe' (num_rows * num_cols) 'und verwenden Sie ein Adressierungsschema wie' data [col_index + (row_index * num_cols)] ''? Weil 'Shape ***' nur schmerzhaft anzusehen ist. –
'***' in C++ ist sehr falsch. –