Constants in a structure

0

I need to implement a circular queue in the following way in c ++, but the following code is Java.

class ColaCircular{
    private int frente;    //frente cola
    int maximo;            //capacidad cola 
    int n;                 //número elementos
    int []vcola;           //vector Cola

    public ColaCircular(int tamano){
    maximo=tamano;             //define capacidad
    vcola=new int [maximo];    //crea espacio en la cola 
    frente=0;                  //inicio variable frente
    n=0;                       //inicio variable número elementos    
    }     
}

Try using this:

struct Cola {
    short frente;
    const short Max_Cola;
    short Cant_Clientes;
    int UnaCola[Max_Cola];

    Cola() : Max_Cola(10) {}
};

But Code :: Blocks 16.01 does not allow it:

error: invalid use of non-static data member 'Cola::Max_Cola'|

Is there any way to do this by declaring the constant within the structure?

    
asked by José F 14.10.2017 в 23:09
source

1 answer

2

The problem is that this:

int UnaCola[Max_Cola];

is not something allowed by the C ++ standard because the value of Max_Cola is not known at compile time (note that it is a variable initialized in the constructor).

You can try something like this:

struct Cola {
    short frente;
    static const short Max_Cola = 10;
    short Cant_Clientes;
    int UnaCola[Max_Cola];

    Cola() {}
};
    
answered by 15.10.2017 в 00:54