Problem to pass archive to function

I'm trying to pass the ifstream archive but it does not compile. And the error messages are too weird I cannot understand the problem itself;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
using namespace std;

int Comp(ifstream archive, int number, int ant, int qtd){
    while(archive >> number){
	if (number == 2*ant){
	    qtd++;
	}
	ant = number;
    }
    archive.close();
    return qtd;
}


int main(){
    int number, qtd = 0;
    int ant = -1;
    
    ifstream archive("numbers.txt");
    
    int i = Comp (archive, number, ant, qtd);
    
    
    return 0;
    }


And, this code receive a archive with some numbers. So, this algorithm tells me how many numbers have your previous number = him/2;
Standard file stream types are not copyable; but they are moveable.

With int Comp( std::ifstream archive, int number, int ant, int qtd ) ;

Either this:
1
2
// rvalue is moved into the function
int i = Comp( std::ifstream("numbers.txt"), number, ant, qtd );


Or the more verbose:
1
2
3
4
std::ifstream archive("numbers.txt");
// force a move
int i = Comp( std::move(archive), number, ant, qtd );
// do not use archive after this 
*If* you can modify your function prototype, perhaps the simplest method is changing it from
int Comp(std::ifstream archive, int number, int ant, int qtd)
to
int Comp(std::ifstream& archive, int number, int ant, int qtd)
Topic archived. No new replies allowed.