How to pass fstream variable to another function?

Asked

Viewed 56 times

0

I am trying to pass a binary file that is already open to a function. However, I get the following error:

error: use of Deleted Function.

Code:

void abreBin(){
    fstream arq_bin("registro.bin", ios::in|ios::binary)
    ....
    // remove é uma funcao do tipo bool
    if(Remove(arq_bin)) // <--- erro acusa nesta linha
        cout << "item removido" << endl;
    else
        cout << "item nao encontrado" << endl;
}

Note: The error only happens when I try to pass the variable arq_bin to the other function.

I tried to use libraries type includes bool, but it didn’t work. I tried to pass other variables to the function and saw that the problem only occurs with variable fstream.

1 answer

0


The mistake probably happens because Remove() has been set to receive a parameter of type fstream by value (copy), but fstream does not allow copying operation.

Alter Remove() so that it receives a parameter by reference, as below:

bool Remove(fstream& arq)

  • Precisely, I discovered this solution yesterday, I was going to answer now kkk. I just did not understand the reason of fstream do not allow copying operation, you know explain?

  • 1

    The copying operation makes no sense for streams. You can understand a stream as an object that "points" to an input/output of data, and does not store it. When copying a stream you would have two objects "pointing" to the same input/output. Imagine that in one of the objects you closed this input/output: you would continue with an object pointing at it, which adds insecurity in the code, if for example you tried to read this input/output that was closed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.