// why does this work?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputStream("scores.txt");
string line;
getline(inputStream, line);
cout << line;
return 0;
}
// but this fails with compiler errors.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream GetStream() {
ifstream inputStream("scores.txt");
return inputStream;
}
int main() {
ifstream inputStream = GetStream();
string line;
getline(inputStream, line);
cout << line;
return 0;
}
compile on mac osx 10.6 w/ g++ 4.2.1
georges-iMac:cs106b george$ g++ help.cpp -o help /usr/include/c++/4.2.1/bits/ios_base.h: In copy constructor ‘std::basic_ios >::basic_ios(const std::basic_ios >&)’: /usr/include/c++/4.2.1/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private /usr/include/c++/4.2.1/iosfwd:55: error: within this context /usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’: /usr/include/c++/4.2.1/iosfwd:89: note: synthesized method ‘std::basic_ios >::basic_ios(const std::basic_ios >&)’ first required here /usr/include/c++/4.2.1/streambuf: In copy constructor ‘std::basic_filebuf >::basic_filebuf(const std::basic_filebuf >&)’: /usr/include/c++/4.2.1/streambuf:794: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits]’ is private /usr/include/c++/4.2.1/iosfwd:86: error: within this context /usr/include/c++/4.2.1/iosfwd: In copy constructor ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’: /usr/include/c++/4.2.1/iosfwd:89: note: synthesized method ‘std::basic_filebuf >::basic_filebuf(const std::basic_filebuf >&)’ first required here help.cpp: In function ‘std::ifstream GetStream()’: help.cpp:9: note: synthesized method ‘std::basic_ifstream >::basic_ifstream(const std::basic_ifstream >&)’ first required here