How to pass variable normally by value through reference?

Asked

Viewed 579 times

1

I want to pass the variable tabe (who’s kind ifstream) by reference to a function (I don’t want a copy of it, just that the function changes it), well, I don’t quite understand how to do it.

Code:

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;


string apaga_espaco(ifstream* tabe)
{

string s;
char N;
while ((tabe*).good())
{
    getline((tabe*), s);
    s.erase(0,29);
    N=s.find(':');
    s.erase(0,N+6);
    return 0;
}

}

 int main()
{
    ifstream tabe;
    char N;

    tabe.open("Tabela.txt", ios::in);

    if (!tabe.is_open())
    {
        cout << "Arquivo nao encontrado, erro fatal!";
        exit(1);
    }

    apaga_espaco(*tabe);

}
  • Do you want to pass value or reference? If you want to pass value, the copy will occur.

  • Why, I want to pass by value, but the compiler returns me this error: 37 22 C: Users Roger Desktop sort main.cpp [Error] no match for 'Operator*' (operand type is 'Std::ifstream {aka Std::basic_ifstream<char>}')

  • What error is shown?

  • [Error] no match for 'Operator*' (operand type is 'Std::ifstream {aka Std::basic_ifstream<char>}')

  • I saw that you accepted both answers. I don’t know if you noticed, you can only accept. So when you accept the second one, you take the first one. There is no problem choosing any of them, but the ideal is to choose which one you thought best. If this is the one you chose, ok, but if it was accepted by accident, you can change the acceptance. See the [tour].

  • I’m new around here, as the two of you helped me I thought I could choose both, but I’m done, thanks!

Show 1 more comment

2 answers

2


I will not look at all your code to see if there are more mistakes. If you gave to understand what you want to do is to pass for reference. Then simply declare the type of the parameter as a reference. The rest works naturally. No need to use pointers.

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

void apaga_espaco(ifstream& tabe) {
    string s;
    char N;
    while (tabe.good()) {
        getline(tabe, s);
        s.erase(0, 29);
        N = s.find(':');
        s.erase(0, N + 6);
    }
}

int main() {
    ifstream tabe;
    char N;
    tabe.open("Tabela.txt", ios::in);
    if (!tabe.is_open()) {
        cout << "Arquivo nao encontrado, erro fatal!";
        exit(1);
    }
    apaga_espaco(tabe);
}

I put in the Github for future reference.

1

Most people who program in c++ have passed through C before, but c++ has some particularities, and one of them is the reference passage. in the C code in order to change the value of the original variable we have to pass the pointer as a parameter in the function, but in C++ we just have to pass the address of this variable that it will be changed, and within our function we can work with the variable as if we were only working with one copy, but we’re actually changing the original variable. then instead of the function signature being like this

string apaga_espaco(ifstream * tabe)

just so it is

void apaga_espaco(ifstream & tabe)

where & means Voce is passing Tabe’s memory address. one thing that got me confused was that you declare a function that returns a string and returns 0 at the end. :(

  • She was bool but then I changed her I forgot to pack, kk

Browser other questions tagged

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