Array bound is not an integer constant before ']' token
-
Hi everyone, I've been running into a little problem now, and I don't know how to get out of it. I'm quite a beginner with QT and c++, and I've searched many forums but still haven't found what I'm looking for.
The idea of this code is to access an element of a velocity-matrix (u, v, and w respectively).
I try to prebuild this matrix with 'fillmatrix' and then try to access an element using 'getMatrixElement'.
Now I've run into this error (see title) and though I know you need to predefine arrays, I don't know how to get my code to work. Who can help me?The error is at lines 18-20 in the header file (where the 3 matrices/arrays are declared)
The main code is:
@#include <cumatrix.h>
#include <iostream>
using namespace std;int main()
{
char u = 'u';
cuMatrix Z;
float x= Z.getMatrixElement(u,2,4);cout<<x<<endl; system("pause"); return 0;
}
@
header file cumatrix.h :
@
#ifndef CLASSES_H
#define CUMATRIX_Hclass cuMatrix
{
public:cuMatrix(void);//constructor ~cuMatrix(void); int setsize(int newSize); float getMatrixElement(char letter,int i,int j);
private:
static int size; float u_matrix[size][size], v_matrix[size][size], w_matrix[size][size]; // declare u,v,w-matrices void fillMatrices(int size);
};
#endif // CUMATRIX_H
@The source file cumatrix.cpp
@
#include <cumatrix.h>cuMatrix::cuMatrix(void)
{
setsize(16); //default size
fillMatrices(size);
}cuMatrix::~cuMatrix(void)
{}
int cuMatrix::setsize(int newSize)
{
size=newSize;
}void cuMatrix::fillMatrices(int size)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
u_matrix[i][j]=2i+3jj; //fill u-matrix
v_matrix[i][j]=5i+0.2j; //fill v-matrix
w_matrix[i][j]=-ii+j*j; // fill w-matrix (vorticity)
}
}
}float cuMatrix::getMatrixElement(char letter, int i, int j)
{float element; switch(letter) { case 'u': element=u_matrix[i][j]; case 'v': element=v_matrix[i][j]; case 'w': element=w_matrix[i][j]; } return element;
}
@
-
Hi,
Think about what you ask the compiler to do in your class definition at line 18..20: Please be so kind to allocate memory for three Arrays of floats with a not yet known size.......................................answer: Huh?? How many???
There are multiple options, but one would be to only define a float pointer to it and in your constructor allocate the buffer and and the pointer to it. Something like:
@
m_MyFloatArray = new float[Size][Size];
@
That should do the trick. IMO it might be better to try and find ways to work with QList/QMap/QVector etc. These classes are designed for array usage and offer many handy and safe working options.
Greetz -
I understand that the compiler doesn't like unknown-size arrays.
Could you explain a little further how to implement, because I don't know where to start?"There are multiple options, but one would be to only define a float pointer to it and in your constructor allocate the buffer and and the pointer to it. Something like:
m_MyFloatArray = new float[Size][Size];
"
In the meantime I will explore IMO options
-
Oke, since your just starting, a complete example:
@
// header file with class definition:
class cuMatrix
{
public:cuMatrix(int SizeToSet = 1);//constructor -> Add size here when constructed ~cuMatrix(void); int setsize(int newSize); float getMatrixElement(char letter,int i,int j);
private:
static int size; // Using static here is very, very error prone! float * u_matrix; // Just pointers allocated! float * v_matrix; float * w_matrix; // declare u,v,w-matrices void fillMatrices(int size);
};
// cpp:
cuMatrix::cuMatrix(int SizeToSet)
{
// Generate your float arrays here!
u_matrix = new float[SizeToSet][SizeToSet];
v_matrix = new float[SizeToSet][SizeToSet];
w_matrix = new float[SizeToSet][SizeToSet];
}
@
The only part that really needs protection is when you alter the static Size variable. When you set a new size, you need to reallocate (so first delete the entire table, then allocate again with the new size!). Always when reading or writing to these tables protect yourself from buffer overflows/underruns!! -
Thanks, I think I see what you mean.
I've used your changes, but now it gives the error:
"array size in operator new must be a constant"
for the lines :
@
u_matrix = new float[SizeToSet][SizeToSet];
v_matrix = new float[SizeToSet][SizeToSet];
w_matrix = new float[SizeToSet][SizeToSet];
@ -
Dammmm, my mistake:
bq. NOTE: The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.Hmm, using a container class in Qt is the way to go!! Let me get back to you about that!
"Allocating array (not recommended, use Qt containers!!)":http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-newAh, found some helpfull tips, but this is it for you:
@
QVector <QMap<int, int> > MyArray;
@
That's it and your are able to use the Qt container class functions. -
All I'm thinking is that the size IS known from the start, the program is meant to run with just one size of the grid, wouldn't
@QVector <QMap<int, int> > MyArray@
be a little too much of a way around? -
Euh, oke, then changing the size variable into a const static variable will do the trick ;-)
Still, the Qt containers are much better to use and even support [] indexes.
But then use a const int size = 10; above your class definition and the size variable in your class definition to create the proper arrays.
The only thing I would like to point out is that defining the arrays in the class generates float arrays on the stack, not the heap. The stack for every program is limited, so it's good practice to allocate data on the heap using new syntax.
Also a #define ARRAY_SIZE 10 could be used ;-)
Still, IMO generate the number of places in the Qt container classes you want to use ;-)
Greetz -
Ok nice, it works now :).
All I'm thinking is how I can use the heap with this example.
This is only a small part of a much larger program, which is going to need to be fast, so I would love to learn more about it.
Could you give me a headstart?