Topic: Is it VC++ or I am going crazy ???
Author: "Dark Digit" <tom@damnedcause.com>
Date: 25 Jan 2001 07:22:56 -0500 Raw View
Your error is in the include! Use fstream.h instead of just fstream.
Shanir <shanir@netvision.net.il> wrote in message
news:93sume$6af$1@news.netvision.net.il...
> Hi all
> anyone knows why this (stupid) code doesn't compile under VC++ (SP4) ??
> I don't think I did somthing wrong , did I ??
>
> #include <fstream>
> class Test {
> public:
> Test(int = 0){};
> void Write(std::ofstream& out) const;
> };
>
> std::ofstream&
> operator<<(std::ofstream& outstream, const Test& bor);
>
>
> void Test::Write(std::ofstream& out) const
> { out << 5; }
>
>
>
> [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]
>
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
> [ Note that the FAQ URL has changed! Please update your bookmarks. ]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 26 Jan 01 03:48:56 GMT Raw View
Dark Digit wrote:
>
> Your error is in the include! Use fstream.h instead of just fstream.
Is this another way in which VC++ is non-conforming? There is no
<fstream.h> in the C++ standard.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 26 Jan 01 03:49:35 GMT Raw View
In article <94libe$jag@dispatch.concentric.net>,
"Dark Digit" <tom@damnedcause.com> wrote:
> Your error is in the include! Use fstream.h instead of just fstream.
Uh, no, "fstream.h" is a non-standard header. fstream is the appropriate
header to include.
[snip]
--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes
Sent via Deja.com
http://www.deja.com/
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <James.Kanze@dresdner-bank.com>
Date: 26 Jan 01 17:23:02 GMT Raw View
Jim Hyslop wrote:
> In article <94libe$jag@dispatch.concentric.net>,
> "Dark Digit" <tom@damnedcause.com> wrote:
> > Your error is in the include! Use fstream.h instead of just fstream.
> Uh, no, "fstream.h" is a non-standard header. fstream is the
> appropriate header to include.
It depends. Use <fstream> if you want to conform to the standard,
<fstream.h> is you want to be portable. Be aware that they DON'T
define the same things, however. The original code used the classes
from <fstream> (in std::), not from <fstream.h> (in ::).
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <James.Kanze@dresdner-bank.com>
Date: 26 Jan 01 17:24:03 GMT Raw View
James Kuyper wrote:
> Dark Digit wrote:
> > Your error is in the include! Use fstream.h instead of just fstream.
> Is this another way in which VC++ is non-conforming? There is no
> <fstream.h> in the C++ standard.
No, but having extensions doesn't render a compiler non-conforming.
(Every Unix compiler I know has unistd.h, which isn't in the C++
standard either.)
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Michael Rubenstein <mike@mrubenstein.com>
Date: 26 Jan 01 17:31:15 GMT Raw View
On 25 Jan 2001 07:22:56 -0500, "Dark Digit" <tom@damnedcause.com>
wrote:
>Your error is in the include! Use fstream.h instead of just fstream.
No. <fstream.h> is nonstandard. The standard <fstream> should
be strongly preferred.
Furthermore, he is using namespaces which are not supported by
Microsoft's <fstream.h>.
Others have correctly identified the problem.
--
Michael M Rubenstein
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Michael S. Terrazas" <michael.s.terrazas@worldnet.att.net>
Date: 26 Jan 2001 17:39:35 -0500 Raw View
"James Kuyper" <kuyper@wizard.net> wrote in message
news:3A702562.E20B63B7@wizard.net...
> Dark Digit wrote:
> >
> > Your error is in the include! Use fstream.h instead of just fstream.
>
> Is this another way in which VC++ is non-conforming? There is no
> <fstream.h> in the C++ standard.
The version of VC++ in the OP was 4.0 namespace std was not in
VC++ until 4.2, IIRC.
--
Mike Terrazas
Brainbench MVP for C++
http://www.brainbench.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 27 Jan 2001 13:39:20 -0500 Raw View
James Kanze wrote:
>
> James Kuyper wrote:
>
> > Dark Digit wrote:
>
> > > Your error is in the include! Use fstream.h instead of just fstream.
>
> > Is this another way in which VC++ is non-conforming? There is no
> > <fstream.h> in the C++ standard.
>
> No, but having extensions doesn't render a compiler non-conforming.
> (Every Unix compiler I know has unistd.h, which isn't in the C++
> standard either.)
True. However, on any conforming implementation, that code should have
compiled as written. An implementation's free to provide <fstream.h>,
but it's not allowed to require it for such code. If #include
<fstream.h> is required in order for the compilation to succeed, then
the implementation is non-conforming.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 27 Jan 2001 13:39:45 -0500 Raw View
In article <3A714210.40B182E3@dresdner-bank.com>,
James Kanze <James.Kanze@dresdner-bank.com> wrote:
>Jim Hyslop wrote:
>> In article <94libe$jag@dispatch.concentric.net>,
>> "Dark Digit" <tom@damnedcause.com> wrote:
>> > Your error is in the include! Use fstream.h instead of just fstream.
>> Uh, no, "fstream.h" is a non-standard header. fstream is the
>> appropriate header to include.
>
>It depends. Use <fstream> if you want to conform to the standard,
><fstream.h> is you want to be portable. Be aware that they DON'T
>define the same things, however. The original code used the classes
>from <fstream> (in std::), not from <fstream.h> (in ::).
It depends. <fstream> is definitely as per Standard C++.
I think we all agree on that.
But anything else is sorta anything else. Sure, you may
_want_ to use <fstream.h> to be portable, and enough implementations
support it, but that doesn't mean your wants will always be met,
or that you'll get what you want if it finds that header.
--
Greg Comeau Comeau C/C++ 4.2.44 "so close"
ONLINE COMPILER ==> http://www.comeaucomputing.com/tryitout
comeau@comeaucomputing.com http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Ron Natalie <ron@spamcop.net>
Date: 27 Jan 2001 13:40:10 -0500 Raw View
James Kanze wrote:
>
> Jim Hyslop wrote:
>
> > In article <94libe$jag@dispatch.concentric.net>,
> > "Dark Digit" <tom@damnedcause.com> wrote:
>
> > > Your error is in the include! Use fstream.h instead of just fstream.
>
> > Uh, no, "fstream.h" is a non-standard header. fstream is the
> > appropriate header to include.
>
> It depends. Use <fstream> if you want to conform to the standard,
> <fstream.h> is you want to be portable. Be aware that they DON'T
> define the same things, however. The original code used the classes
> from <fstream> (in std::), not from <fstream.h> (in ::).
fstream.h isn't exactly portable either.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: James Kanze <kanze@gabi-soft.de>
Date: 29 Jan 01 03:00:22 GMT Raw View
Ron Natalie <ron@spamcop.net> writes:
|> James Kanze wrote:
|> > Jim Hyslop wrote:
|> > > In article <94libe$jag@dispatch.concentric.net>,
|> > > "Dark Digit" <tom@damnedcause.com> wrote:
|> > > > Your error is in the include! Use fstream.h instead of just fstream.
|> > > Uh, no, "fstream.h" is a non-standard header. fstream is the
|> > > appropriate header to include.
|> > It depends. Use <fstream> if you want to conform to the standard,
|> > <fstream.h> is you want to be portable. Be aware that they DON'T
|> > define the same things, however. The original code used the
|> > classes from <fstream> (in std::), not from <fstream.h> (in ::).
|> fstream.h isn't exactly portable either.
100%, no. More than <fstream>, yes.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <kanze@gabi-soft.de>
Date: 29 Jan 01 03:20:50 GMT Raw View
comeau@panix.com (Greg Comeau) writes:
|> In article <3A714210.40B182E3@dresdner-bank.com>,
|> James Kanze <James.Kanze@dresdner-bank.com> wrote:
|> >It depends. Use <fstream> if you want to conform to the standard,
|> ><fstream.h> is you want to be portable. Be aware that they DON'T
|> >define the same things, however. The original code used the
|> >classes from <fstream> (in std::), not from <fstream.h> (in ::).
|> It depends. <fstream> is definitely as per Standard C++. I think
|> we all agree on that.
Certainly. I think we all agree that not all compilers are conformant,
either:-). I'd love to use export, as well.
|> But anything else is sorta anything else. Sure, you may _want_ to
|> use <fstream.h> to be portable, and enough implementations support
|> it, but that doesn't mean your wants will always be met, or that
|> you'll get what you want if it finds that header.
In practice, MORE compilers support <fstream.h> than support <fstream>.
Or perhaps I mean to say that there are less differences between the
different implementations of <fstream.h> than there are between the
implementations of <fstream>, given that one major implementer provides
an <fstream> which is actually an <fstream.h>.
This is, of course, the situation today. It is subject to change.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <kanze@gabi-soft.de>
Date: 29 Jan 01 03:28:55 GMT Raw View
James Kuyper <kuyper@wizard.net> writes:
|> James Kanze wrote:
|> > James Kuyper wrote:
|> > > Dark Digit wrote:
|> > > > Your error is in the include! Use fstream.h instead of just
|> > > > fstream.
|> > > Is this another way in which VC++ is non-conforming? There is no
|> > > <fstream.h> in the C++ standard.
|> > No, but having extensions doesn't render a compiler
|> > non-conforming. (Every Unix compiler I know has unistd.h, which
|> > isn't in the C++ standard either.)
|> True. However, on any conforming implementation, that code should
|> have compiled as written.
Certainly. But as I pointed out, changing the code to <fstream.h> won't
change anything. In this respect (the library), at least, VC++ is more
conforming than most.
|> An implementation's free to provide <fstream.h>, but it's not
|> allowed to require it for such code. If #include <fstream.h> is
|> required in order for the compilation to succeed, then the
|> implementation is non-conforming.
Yes and no. If you use extension (like the classic iostream), then the
implementation can require special files for them to work. (Again, this
is no different from <unistd.h>.)
All of which is irrelevant to the original posting, of course. The
original code used <fstream>, and the new iostream. Changing it to use
<fstream.h> (without changing anything else) will generate a lot more
errors, since the <fstream.h> of VC++ doesn't put the streams in std::.
(It's an extension for backwards compatibility, so this seems reasonable
to me.)
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kanze <James.Kanze@dresdner-bank.com>
Date: 29 Jan 01 13:56:22 GMT Raw View
"Michael S. Terrazas" wrote:
> "James Kuyper" <kuyper@wizard.net> wrote in message
> news:3A702562.E20B63B7@wizard.net...
> > Dark Digit wrote:
> > > Your error is in the include! Use fstream.h instead of just fstream.
> > Is this another way in which VC++ is non-conforming? There is no
> > <fstream.h> in the C++ standard.
> The version of VC++ in the OP was 4.0 namespace std was not in VC++
> until 4.2, IIRC.
The OP spoke of SP4, which I assume meant service pack 4 (of VC++ 5.0
or 6.0).
At any rate, if he includes <fstream>, he needs std::; if he includes
<fstream.h>, he shouldn't use it.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kuyper <kuyper@wizard.net>
Date: 29 Jan 2001 18:35:18 -0500 Raw View
James Kanze wrote:
>
> James Kuyper <kuyper@wizard.net> writes:
...
> |> True. However, on any conforming implementation, that code should
> |> have compiled as written.
>
> Certainly. But as I pointed out, changing the code to <fstream.h> won't
> change anything. In this respect (the library), at least, VC++ is more
> conforming than most.
It was claimed that changing the code to use <fstream.h> was required to
make it compile. If true, that's non-conforming.
> |> An implementation's free to provide <fstream.h>, but it's not
> |> allowed to require it for such code. If #include <fstream.h> is
> |> required in order for the compilation to succeed, then the
> |> implementation is non-conforming.
>
> Yes and no. If you use extension (like the classic iostream), then the
> implementation can require special files for them to work. (Again, this
> is no different from <unistd.h>.)
Yes, but as far as I could tell, he was making no use of classic
iostream.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: James Kanze <James.Kanze@dresdner-bank.com>
Date: 30 Jan 01 17:33:31 GMT Raw View
James Kuyper wrote:
> James Kanze wrote:
> > James Kuyper <kuyper@wizard.net> writes:
> ...
> > |> True. However, on any conforming implementation, that code should
> > |> have compiled as written.
> > Certainly. But as I pointed out, changing the code to <fstream.h> won't
> > change anything. In this respect (the library), at least, VC++ is more
> > conforming than most.
> It was claimed that changing the code to use <fstream.h> was
> required to make it compile. If true, that's non-conforming.
Agreed, but since it's not true, irrelevant.
I do have code in which you have to use <fstream.h>, rather than
<fstream>. It's certainly not conformant to the current standard.
But it works on more compilers than conformant code would, so I'm not
changing it. Yet. (Actually, the only compiler holding me up is g++,
and it has been promised that in March...)
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: James Kuyper <kuyper@wizard.net>
Date: 31 Jan 2001 13:09:41 -0500 Raw View
James Kanze wrote:
>
> James Kuyper wrote:
...
> > It was claimed that changing the code to use <fstream.h> was
> > required to make it compile. If true, that's non-conforming.
>
> Agreed, but since it's not true, irrelevant.
It's quite relevant, because every message of mine on this sub-thread
has been about that claim. Your messages on this thread have been about
legal extensions. Those messages only seem relevant me as responses to
my messages, if you were trying to suggest that the claim described
behavior which would be a legal extension to a conforming compiler. If
that's not what you were suggesting, why did you bring up the issue of
extensions on this thread?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Shanir" <shanir@netvision.net.il>
Date: 17 Jan 01 17:38:41 GMT Raw View
Hi all
anyone knows why this (stupid) code doesn't compile under VC++ (SP4) ??
I don't think I did somthing wrong , did I ??
#include <fstream>
class Test {
public:
Test(int = 0){};
void Write(std::ofstream& out) const;
};
std::ofstream&
operator<<(std::ofstream& outstream, const Test& bor);
void Test::Write(std::ofstream& out) const
{ out << 5; }
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@spamcop.net>
Date: 18 Jan 2001 07:23:35 -0500 Raw View
Shanir wrote:
>
> Hi all
> anyone knows why this (stupid) code doesn't compile under VC++ (SP4) ??
> I don't think I did somthing wrong , did I ??
You did. It's not VC++'s fault.
>
> #include <fstream>
> class Test {
> public:
> Test(int = 0){};
> void Write(std::ofstream& out) const;
> };
>
> std::ofstream&
> operator<<(std::ofstream& outstream, const Test& bor);
>
> void Test::Write(std::ofstream& out) const
> { out << 5; }
ofstream& << (int) is ambiguous.
The problem is that it could convert the left-hand-side from
ofstream -> ostream
-OR-
It could converted the right hand side from int to Test (using
the Test constructor that takes an int) and then use the
ofstream << Test operator you wrote above.
You could fix this by making the Test(int) constructor explicit.
You could also fix it (and I think this is going to be better for
you in the long run) by replacing "ofstream" with "ostream".
It fixes the ambiguity problem, and is also of greater utility.
You can the use the "<< Test" operator on any sort of output,
not just files. With what you wrote you can't do:
Text x;
cout << x;
even...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 18 Jan 2001 07:53:55 -0500 Raw View
In article <93sume$6af$1@news.netvision.net.il>,
"Shanir" <shanir@netvision.net.il> wrote:
> Hi all
> anyone knows why this (stupid) code doesn't compile under VC++ (SP4)
??
> I don't think I did somthing wrong , did I ??
Can I give you a hint? Next time, include some indication of what the
error message was. Fortunately, I have VC++ and I was able to reproduce
your problem very easily.
> #include <fstream>
> class Test {
> public:
> Test(int = 0){};
> void Write(std::ofstream& out) const;
> };
>
> std::ofstream&
> operator<<(std::ofstream& outstream, const Test& bor);
>
> void Test::Write(std::ofstream& out) const
> { out << 5; }
(For those without the aforementioned compiler, the error is on the last
line, "Error c2666: '<<' : 13 overloads have similar conversions")
The line "out << 5" is ambiguous. Do you want the compiler to implicitly
convert the number 5 to a Test object using T::T(int), and call the
overloaded insertion operator you wrote? Or do you want the compiler to
call operator <<(std::ofstream &, int)? Or one of the other 11 possible
conversion sequences?
(side question - anyone care to elaborate on the 13 possible conversions
the compiler is referring to?)
The solution is to follow Scott Meyer's advice: Be wary of user-defined
conversion functions ("More Effective C++", available on CD - this
particular topic is also online at
http://www.awlonline.com/cseng/meyerscddemo/DEMO/MEC/MI5_FR.HTM)
The solution, by the way, is simple - make the conversion ctor explicit,
i.e.:
class Test
{
public:
explicit Test(int =0){};
};
I don't *think* this will interfere with the creation of automatic
variables, but if it does you then add a default constructor and remove
the default value for the integer parameter, i.e.:
class Test
{
public:
Test() {/* whatever */}
explicit Test(int) {/* whatever */}
};
--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes
Sent via Deja.com
http://www.deja.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]