Re: Help needed for STL ifstream class
Kira Yamato wrote:
I've posted this in another thread, but I suppose I should've started a
new thread for it instead.
I cannot get the following short program to compile under g++:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
copy(istream_iterator<char>(argc >= 2 ? ifstream(argv[1]) : cin), //
this line won't compile!
istream_iterator<char>(),
ostream_iterator<char>(cout));
return 0;
}
The compiler error messages are as followed:
/usr/include/c++/4.0.0/iosfwd: In copy constructor 'std::basic_ios<char,
std::char_traits<char> >::basic_ios(const std::basic_ios<char,
std::char_traits<char> >&)':
/usr/include/c++/4.0.0/bits/ios_base.h:779: error:
'std::ios_base::ios_base(const std::ios_base&)' is private
/usr/include/c++/4.0.0/iosfwd:55: error: within this context
/usr/include/c++/4.0.0/iosfwd: In copy constructor
'std::basic_istream<char, std::char_traits<char> >::basic_istream(const
std::basic_istream<char, std::char_traits<char> >&)':
/usr/include/c++/4.0.0/iosfwd:61: warning: synthesized method
'std::basic_ios<char, std::char_traits<char> >::basic_ios(const
std::basic_ios<char, std::char_traits<char> >&)' first required here
a.cpp: In function 'int main(int, char**)':
a.cpp:10: warning: synthesized method 'std::basic_istream<char,
std::char_traits<char> >::basic_istream(const std::basic_istream<char,
std::char_traits<char> >&)' first required here
a.cpp:10: error: no matching function for call to
'std::istream_iterator<char, char, std::char_traits<char>,
ptrdiff_t>::istream_iterator(std::basic_istream<char,
std::char_traits<char> >)'
/usr/include/c++/4.0.0/bits/stream_iterator.h:70: note: candidates are:
std::istream_iterator<_Tp, _CharT, _Traits,
_Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT,
_Traits, _Dist>&) [with _Tp = char, _CharT = char, _Traits =
std::char_traits<char>, _Dist = ptrdiff_t]
/usr/include/c++/4.0.0/bits/stream_iterator.h:66: note:
std::istream_iterator<_Tp, _CharT, _Traits,
_Dist>::istream_iterator(std::basic_istream<_CharT, _Traits>&) [with _Tp
= char, _CharT = char, _Traits = std::char_traits<char>, _Dist = ptrdiff_t]
/usr/include/c++/4.0.0/bits/stream_iterator.h:62: note:
std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator()
[with _Tp = char, _CharT = char, _Traits = std::char_traits<char>, _Dist
= ptrdiff_t]
Now, I have discovered that if I change the program into the following,
then it compiles fine:
#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;
int main(int argc, char **argv)
{
istream *ifile = argc >= 2 ? new ifstream(argv[1]) : &cin;
copy(istream_iterator<char>(*ifile),
istream_iterator<char>(),
ostream_iterator<char>(cout));
return 0;
}
I know this works, but it would be nice to understand why the original
version does not work anyway.
Thank you for your help.
As "? :" need the second and third expression to be of the same type
The first example is trying to cast "ifstream(argv[1])" to "ostream&".
But "ifstream(argv[1])" is an rvalue, you can't cast it to lvalue,
Unless "Rvalue Reference" is allowed