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.
– Maniero
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>}')
– Roger Amaro Almeida
What error is shown?
– Maniero
[Error] no match for 'Operator*' (operand type is 'Std::ifstream {aka Std::basic_ifstream<char>}')
– Roger Amaro Almeida
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].
– Maniero
I’m new around here, as the two of you helped me I thought I could choose both, but I’m done, thanks!
– Roger Amaro Almeida