Is there a way to pass in a ofstream reference to a object?

JayB

I'm trying to pass a reference to an ofstream with an open file to an object, so that it's functions can also print to the file.

When I try to compile my program, it says that all reference members must be initialized, and I read online that streams cannot be reassigned. So what do I do?

Here's what I have for my constructors:

GameShow::GameShow(int numElements){

    // Initialize heap
    v = new Contestant[numElements+1];
    capacity = numElements+1;
    size = 0;
}


GameShow(int numElements, std::ofstream &of)
: outFile(of){

        // Initialize heap
    v = new Contestant[numElements+1];
    capacity = numElements+1;
    size = 0;
    outputToFile = true;
    handle.reserve(numElements+1);
    handle.resize(numElements+1, -1);
}

And here is my declaration in my header file:

// Members
....
ofstream &outFile;
....
GameShow(int numElements);
GameShow(int numElements, std::ofstream &of);
....

I have the ofstream open in my main() function, but my object's functions need to be able to modify the same file... I feel like I've tried everything.

When I tried passing in the name of the file and trying to open it in append mode in the object and print to it, the output was all out of order and completely out of sync with the output from my main function. It seems as though all of the print statements I call from my object are being held in a buffer until my main function has closed the stream on its end. Any help would be greatly appreciated.

Attempting to use the constructor in my main function:

       // Attempt to open output file
    ofstream outFile;
    outFile.open(outFileName);

    if(inFile.is_open()){
            if(outFile.is_open()){
                    // Get information
                    int numContestants = 0;
                    inFile >> numContestants;


                    // Process file
                    if(numContestants > 0){
                            GameShow gs(numContestants, outFile);

Errors (the only one im getting):

GameShow.cpp: In constructor ‘GameShow::GameShow(int)’:
GameShow.cpp:27:1: error: uninitialized reference member in ‘std::ofstream& {aka class std::basic_ofstream<char>&}’ [-fpermissive]
 GameShow::GameShow(int numElements){
 ^
GameShow.h:14:18: note: ‘std::ofstream& GameShow::outFile’ should be initialized
std::ofstream &outFile;
              ^
make: *** [GameShow.o] Error 1
emlai

Reference members cannot be left uninitialized. You need to initialize outFile in the GameShow(int numElements) constructor as well.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Pass a reference to DOM object with ng-click

From Java

Correct way to pass prototype to an object

From Dev

Is there a way to pass a primitive parameter by reference in Dart?

From Dev

Is there a way to pass a method reference in Java?

From Dev

c# object pass by reference or pass by value

From Dev

Is passing object to constructor pass by reference

From Dev

How to pass object by reference to a constructor

From Dev

JAVA Returning an object or pass by reference

From Dev

Java - Pass by Reference for the Byte object

From Dev

Is this a devious way to pass by reference in JavaScript?

From Dev

pass by reference a mat object OpenCv

From Dev

Can't pass temporary object as reference

From Dev

Pass by reference avoids object slicing

From Dev

C++ 11 correct way to pass a functional object by reference or pointer?

From Dev

Is there a way to pass a copy of the object rather than reference to Java method

From Dev

C++ Correct way to pass a std::unique_ptr object as reference on a function argument

From Dev

How to pass a PHP object NOT by reference

From Dev

c# object pass by reference or pass by value

From Dev

Is there a way to use a 'pass by reference' in a class constructor?

From Dev

Java - Pass by Reference for the Byte object

From Dev

Efficient and clean way to write an ofstream/fstream object for writing tables with headers

From Dev

Is there another way to pass an object reference to an javascript anonymous function?

From Dev

Pass new object or object reference

From Dev

Is there a way to pass a copy of the object rather than reference to Java method

From Dev

Pass reference by ambiguous object

From Dev

Pass-by-object-reference and mutability

From Dev

Pass object by reference to a different object

From Dev

ofstream reference using string?

From Dev

Unable to pass reference of object

Related Related

  1. 1

    Pass a reference to DOM object with ng-click

  2. 2

    Correct way to pass prototype to an object

  3. 3

    Is there a way to pass a primitive parameter by reference in Dart?

  4. 4

    Is there a way to pass a method reference in Java?

  5. 5

    c# object pass by reference or pass by value

  6. 6

    Is passing object to constructor pass by reference

  7. 7

    How to pass object by reference to a constructor

  8. 8

    JAVA Returning an object or pass by reference

  9. 9

    Java - Pass by Reference for the Byte object

  10. 10

    Is this a devious way to pass by reference in JavaScript?

  11. 11

    pass by reference a mat object OpenCv

  12. 12

    Can't pass temporary object as reference

  13. 13

    Pass by reference avoids object slicing

  14. 14

    C++ 11 correct way to pass a functional object by reference or pointer?

  15. 15

    Is there a way to pass a copy of the object rather than reference to Java method

  16. 16

    C++ Correct way to pass a std::unique_ptr object as reference on a function argument

  17. 17

    How to pass a PHP object NOT by reference

  18. 18

    c# object pass by reference or pass by value

  19. 19

    Is there a way to use a 'pass by reference' in a class constructor?

  20. 20

    Java - Pass by Reference for the Byte object

  21. 21

    Efficient and clean way to write an ofstream/fstream object for writing tables with headers

  22. 22

    Is there another way to pass an object reference to an javascript anonymous function?

  23. 23

    Pass new object or object reference

  24. 24

    Is there a way to pass a copy of the object rather than reference to Java method

  25. 25

    Pass reference by ambiguous object

  26. 26

    Pass-by-object-reference and mutability

  27. 27

    Pass object by reference to a different object

  28. 28

    ofstream reference using string?

  29. 29

    Unable to pass reference of object

HotTag

Archive