Doubt about decoupling algorithm with templates

0

I have a situation with a queue that I made with templates

When I fill the queue with data and then when I undo an item I can not re-enter any element, because it tells me that the queue is full

This is the header link

This is the function that unravels

template <class T>
int Cola<T>::operator -(T *Dato)
{
/* La variable Res se inicializa en 0 (fracaso). Si la eliminación se
lleva a cabo, entonces se le asignará el valor de 1 (éxito). */
int Res = 0;
int i;

if (!ColaVacia())
{
    *Dato = EspacioCola[Frente];
    if (Frente == Final)
    {
        Frente = -1;
        Final  = -1;
    }
    else
        Frente++;            
    Res = 1;
}
return Res;
 }

link

This is the main this calls the function in this way

  case 2:
            if(Deposito - &Prod)
            {
                cout << Prod;
                Total = Total+Prod.RegresaPrecio();
            }
            else
                cout<<"\n\tNo se encuentran productos en el deposito !!!\n";
            break;

I understand that the indicated "End" of the queue has to be moved to give space, but how do I do that?

    
asked by Marco Leslie 17.10.2017 в 05:25
source

1 answer

2

With the incomplete code and without telling us what your data collection is, we can only play the riddles.

Your interface is terrible.

The binary subtraction operator is the worst choice you could make .

The interface with which you access the methods of your template class Cola seriously attacks the principle of the least surprise Apparently to unbind an object from your queue, you use the binary subtraction operator (binary because it receives two operators, one on each side), which is already terribly counterintuitive to begin with, but you worsen it by using a pointer to the data instead of the data in itself.

The binary subtraction operator does not modify any of the data on which it is applied, let's see it with an example:

int a = 2;
int b = 2;
int c = a - b;

In the previous code, the binary subtraction operator is applied over the entire variables a and b and the result is saved in the entire variable c , after operating we have the following values:

std::cout << a << ' ' << b << ' ' << c; // Muestra 2 2 0

As you can see, the subtraction operator ( - ) binary has not modified the variables a or b . Instead in your code, the instruction:

if(Deposito - &Prod)

The operator modifies the variable to its left ( Deposito ) which is contrary to what can be expected from that type of operator (undermining the principle of minimum surprise).

Use another operator or better: use a member function .

If you really want to use an operator, you overload the operator -= :

template <class T>
int Cola<T>::operator -=(T *Dato)
//                    ^^ <--- Sabemos que este operador modifica el dato de la izquierda

if(Deposito -= &Prod)
// ^^^^^^^^^^^ <--- Queda clara la intencion de modificar Deposito

But I do not advise you, it can seem very clear and clean the code using operators, but if you give them a different meaning to the one that is intuitively known for them, you will make the code confusing for other programmers; So: use a function that makes clear what your mission is by choosing a clear and descriptive name for it:

template <class T>
bool Cola<T>::desencola(T *Dato)
{
    /* La variable Res se inicializa en false (fracaso). Si la eliminación se
    lleva a cabo, entonces se le asignará el valor de true (éxito). */
    int Res = false;

    // ....

    return Res;
}

You will notice that I have changed the name of the undocking function (before an operator) and this new name leaves no doubt about the mission that it performs, I have also changed the boolean return, since the result of undoing can only be be true or false.

Do not pass a pointer

Your Cola stores data, not pointers, so to unpin a pointer is very counterintuitive, you should change it by the data you want to undo:

template <class T>
bool Cola<T>::desencola(const T &Dato)
{
    /* La variable Res se inicializa en false (fracaso). Si la eliminación se
    lleva a cabo, entonces se le asignará el valor de true (éxito). */
    int Res = false;

    // ....

    return Res;
}

You will notice that I have changed the type that receives the function to const T & because what you want is to delete the data type T contained in your Cola without modifying the last data.

Your problem.

A pointer is a memory address to a variable, two different variables that contain the same value, will have different pointers. Given that I lack context I can not say what I am going to say, but I think your problem is that you are not undoing anything, and you do not shrink anything because when you pass the pointer to undock:

case 2:
    if(Deposito - &Prod)

You pass the pointer to a local variable that obviously will not be contained in your Cola .

    
answered by 18.10.2017 в 21:58