* fstream/ios (hopefully not OT)
@ 2003-01-30 0:19 Wagner Matthias
2003-01-30 12:50 ` John Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: Wagner Matthias @ 2003-01-30 0:19 UTC (permalink / raw)
To: gcc-help
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
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: fstream/ios (hopefully not OT)
2003-01-30 0:19 fstream/ios (hopefully not OT) Wagner Matthias
@ 2003-01-30 12:50 ` John Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: John Love-Jensen @ 2003-01-30 12:50 UTC (permalink / raw)
To: Wagner Matthias, gcc-help
Hi Wagner,
You have something really unusual (in an odd / bad way) in your class. You
shouldn't use the operator << on the ofstream, you should use it on the
ostream.
If you want a binary output (not indicated by your example, but just in
case), you shouldn't use the operator << mechanism, rather you should create
a read method for input, and a write method for output.
My modified version is below, and the problem goes away.
Sincerely,
--Eljay
---- CLASS LISTING
#include <iostream>
using std::ostream;
class Test
{
private:
int m;
int n;
public:
Test()
: m(0), n(0) { }
Test(int a, int b = 0)
: m(a), n(b) { }
friend ostream& operator << (ostream& ofstr, const Test& a);
};
ostream& operator << (ostream& ofstr, const Test &a)
{
ofstr << a.m;
return ofstr;
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2003-01-30 12:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-30 0:19 fstream/ios (hopefully not OT) Wagner Matthias
2003-01-30 12:50 ` John Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).