Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Array bound is not an integer constant before ']' token
Forum Updated to NodeBB v4.3 + New Features

Array bound is not an integer constant before ']' token

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 13.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JerichoJosh
    wrote on 6 Dec 2013, 08:47 last edited by
    #1

    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&#40;"pause"&#41;;
    return 0;
    

    }

    @

    header file cumatrix.h :
    @
    #ifndef CLASSES_H
    #define CUMATRIX_H

    class 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]=5
    i+0.2j; //fill v-matrix
    w_matrix[i][j]=-i
    i+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;
    

    }

    @

    1 Reply Last reply
    0
    • J Offline
      J Offline
      Jeroentjehome
      wrote on 6 Dec 2013, 08:54 last edited by
      #2

      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

      Greetz, Jeroen

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JerichoJosh
        wrote on 6 Dec 2013, 09:03 last edited by
        #3

        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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jeroentjehome
          wrote on 6 Dec 2013, 09:49 last edited by
          #4

          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!!

          Greetz, Jeroen

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JerichoJosh
            wrote on 6 Dec 2013, 10:50 last edited by
            #5

            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];
            @

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jeroentjehome
              wrote on 6 Dec 2013, 12:26 last edited by
              #6

              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-new

              Ah, 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.

              Greetz, Jeroen

              1 Reply Last reply
              0
              • J Offline
                J Offline
                JerichoJosh
                wrote on 6 Dec 2013, 12:57 last edited by
                #7

                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?

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Jeroentjehome
                  wrote on 6 Dec 2013, 13:34 last edited by
                  #8

                  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

                  Greetz, Jeroen

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JerichoJosh
                    wrote on 6 Dec 2013, 14:30 last edited by
                    #9

                    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?

                    1 Reply Last reply
                    0

                    1/9

                    6 Dec 2013, 08:47

                    • Login

                    • Login or register to search.
                    1 out of 9
                    • First post
                      1/9
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved