0

We were given a prewritten (mostly) .cc file in which we were to write the implementation for two different functions. I was trying to do this without messing with the header of the function but can't seem to do it unless I am forgetting something since it has been awhile since I've done this.

void NumList::save_to_file(ostream& outs)

I tried using

string filename;

outs.open(filename.c_str());

to open the file but get the error

'std::ostream' has no member name 'open'

Whats the go around with this?

5
  • 6
    Literally, std::ostream does not have open. It is std::ofstream exclusive.
    – user2486888
    Jan 21, 2015 at 1:39
  • 3
    Presumably the file is open somewhere else - all you have to do is stream stuff to outs.
    – Barry
    Jan 21, 2015 at 1:39
  • 1
    open() is only available for file streams. You should have your save_to_file() take std::[x]fstream depending on whether it's for input, output, or both.
    – David G
    Jan 21, 2015 at 1:47
  • assuming that the save_to_file signature is provided for you as part of the assignment, it would also be safe to assume that you will be passed a file stream that is already open
    – Red Alert
    Jan 21, 2015 at 1:49
  • Looks like you don't need to open the file in that function. That is the responsibility of the function caller. All you need to do is the output statements.
    – Galik
    Jan 21, 2015 at 1:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.