Passing ofstream objects to functions

So I know I have to do this by reference. To be honest I have not had any experience with outputting to a file or even inputting from a file.

Here is the function that is throwing me the compiler error.
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
int operations (ofstream &outFile, int num1, int num2, char opChoice)
{
    int result;
    switch (opChoice){
    case '&': outFile << "Applying the AND operator & to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << num2 << " = ";
            printBinary(num2);
            outFile << "\n";
            result = (num1 & num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    case '|': outFile << "Applying the OR operator | to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << num2 << " = ";
            printBinary(num2);
            outFile << "\n";
            result = (num1 | num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    case '^': outFile << "Applying the XOR operator ^ to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << num2 << " = ";
            printBinary(num2);
            outFile << "\n";
            result = (num1 ^ num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    case '~': outFile << "Applying the Inverting operator ~ to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << "\n";
            result = ~num1;
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    case '<': outFile << "Applying the Shift Left operator << to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << num2 << " = ";
            printBinary(num2);
            outFile << "\n";
            result = (num1 << num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    case '>': outFile << "Applying the Shift Right operator >> to:\n" << num1 << " = ";
            printBinary(num1);
            outFile << num2 << " = ";
            printBinary(num2);
            outFile << "\n";
            result = (num1 >> num2);
            outFile << "Produces \n";
            outFile << result << " = ";
            return result;
    }
}


These are the compiler errors.
I am just not sure where the issue is. The only thing I can think of is that I'm using it in a switch statement. But, from the examples that I've seen I have it set up correctly.
/home/nahjil/Desktop/testingprintBinary().cpp||In function ‘int operations(std::ofstream&, int, int, char)’:|
/home/nahjil/Desktop/testingprintBinary().cpp|75|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|77|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|84|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|86|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|93|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|95|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|102|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|109|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|111|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|118|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|120|error: invalid initialization of reference of type ‘std::ofstream& {aka std::basic_ofstream<char>&}’ from expression of type ‘int’|
/home/nahjil/Desktop/testingprintBinary().cpp|9|error: in passing argument 1 of ‘void printBinary(std::ofstream&, int)’|
/home/nahjil/Desktop/testingprintBinary().cpp|127|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 22 errors, 1 warnings ===|

1
2
void printBinary(std::ofstream&, int); //ask for 2 parameters
printBinary(num1); //just 1 is provided 

Thanks I didn't realize I submitted this. When I was typing it out I figured it out.
Topic archived. No new replies allowed.