This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
fstream/ios (hopefully not OT)
- From: Wagner Matthias <necrox at gmx dot at>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 30 Jan 2003 01:22:59 +0100
- Subject: fstream/ios (hopefully not OT)
Hi,
hopefully I am not offtopic here. If so, please ignore my req. for help
and accept my honest apologies.
A friend of mine wrote a program which used fstream by
#include<fstream>, and it didn't work. I wrote a small test-class
(dirty..)
---- CLASS LISTING
#include<cstdio>
#include<cstdlib>
//#include <ios>
#include <fstream>
using namespace::std;
class Test
{
private:
int m, n;
public:
Test() { m = n = 0; }
Test(int a, int b=0)
:m(a), n(b) {};
friend ofstream& operator << (ofstream &ofstr, const Test &a);
};
ofstream & operator << (ofstream &ofstr, const Test &a)
{
ofstr << a.m;
return ofstr;
}
---- G++
# g++ test.cpp
test.cpp: In function `std::ofstream& operator<<(std::ofstream&, const
Test&)':
test.cpp:25: choosing `std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT =
char,
_Traits = std::char_traits<char>]' over `std::ofstream&
operator<<(std::ofstream&, const Test&)'
test.cpp:25: because worst conversion for the former is better than
worst
conversion for the latter
Now, if I include ios instead of fstream, I can compile the class
(actually a can't, as int main(void) is missing, but there is no compiler-
related error anymore).
I always thought fstream included ios, as it is a inheritive class, but as
I read through both files, I found some differences:
/usr/include/c++/3.2/ios includes iosfwd, which then includes files from
./bits/ and defines typedefs
typedef basic_ofstream<char> ofstream;
ios, funnily, never includes something related to ofstream, as far as I
can see, as the structure of all those includes is _very_ chaotic, so I
assume that ios acts as some "virtual base definition" and won't work
stand alone!? "fstream" includes "istream" and "ostream" directly, so I
guess fstream is the implementation for all those typedefs in "iosfwd".
Now why is the class above not working if I include "fstream", but this
class actually compiles without errors or warnings (except for a linker
error (main again))
#include<cstdio>
#include<cstdlib>
#include <ios>
#include <fstream>
using namespace::std;
class Test
{
private:
int m, n;
public:
Test() { m = n = 0; }
friend ofstream& operator << (ofstream &ofstr, const Test &a);
};
ofstream & operator << (ofstream &ofstr, const Test &a)
{
ofstr << a.m;
return ofstr;
}
I only stripped one constructor of "Test"!
May someone simply explain or send me some link how the standard
c++-headers are working? I'm quite confused...
mfg
Nec
--
lacrimae mortis sanguis meus sunt