Topic: [Q] Notation when deriving from std::ostream


Author: kanze@gabi-soft.de (James Kanze)
Date: Thu, 14 Nov 2002 17:28:41 +0000 (UTC)
Raw View
steve_pole@yahoo.com ("Steve Pole") wrote in message
news:<aqtk1k$den9r$1@ID-165031.news.dfncis.de>...

> This works:

> [snip]

> #include <iostream>
> using std::ostream;

> class myStream : public ostream
> {
>     ...
> }

> [snap]

> But this doesn't:

> [snip]

> #include <iostream>

> class myStream : public std::ostream
> {
>     ...
> }

What is the error message?

> [snap]

> implementation:

> [snip]

> myStream::myStream ()
>  : ostream (new myStreamBuffer ())

Without the using, I think you need std::ostream here as well.

>  {
> }

> [snap]

> MSVC6 SP4 / W2K SP2 says 'error C2512: 'basic_ostream<char,struct
> std::char_traits<char> >' : no appropriate default constructor available''
> and 'error C2614: 'myStream' : illegal member initialization: 'ostream' is
> not a base or member' at the init list of the constructor (which can be
> pretty frustrating if you're not lucky enough to know what you changed...)

Without the using, above, ostream is unknown.

I have had problems with this with VC++, although I don't remember
whether it was 5.0 or 6.0.  In my case, however, it was (apparently)
that VC++ didn't accept a typedef for the base class in the
initialization list.  I think that this is a bug, but I did end up
writing:

    MyStream::MyStream()
        :   std::basic_ostream< char >( ... )

A bit of a pain.

> Does this conform to the C++ standard or is this another Microsoft
> 'feature' ? I thought both versions of the declaration were
> interchangable ?? Do I have to think again ??

I think that if you add the std:: in the implementation (when there is
no using), you should be conform.  Not that that helps much if it
doesn't work, and you cannot change compilers.

--
James Kanze                           mailto:jkanze@caicheuvreux.com
Conseils en informatique orient   e objet/
                    Beratung in objektorientierter Datenverarbeitung

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: steve_pole@yahoo.com ("Steve Pole")
Date: Thu, 14 Nov 2002 20:09:49 +0000 (UTC)
Raw View
Hi James,

thanks for your help, please see inline...

"James Kanze" <kanze@gabi-soft.de> schrieb im Newsbeitrag

news:<d6651fb6.0211140155.415f8a7f@posting.google.com>...

> >

> > myStream::myStream ()

> > : ostream (new myStreamBuffer ())

>

> Without the using, I think you need std::ostream here as well.

>

Hmmm, tried that too, but the error remains the same.



> > [snap]

>

> > MSVC6 SP4 / W2K SP2 says 'error C2512: 'basic_ostream<char,struct

> > std::char_traits<char> >' : no appropriate default constructor

available''

> > and 'error C2614: 'myStream' : illegal member initialization: 'ostream'

is

> > not a base or member' at the init list of the constructor (which can be

> > pretty frustrating if you're not lucky enough to know what you

changed...)

>

> Without the using, above, ostream is unknown.

>

> I have had problems with this with VC++, although I don't remember

> whether it was 5.0 or 6.0. In my case, however, it was (apparently)

> that VC++ didn't accept a typedef for the base class in the

> initialization list. I think that this is a bug, but I did end up

> writing:

>

> MyStream::MyStream()

> : std::basic_ostream< char >( ... )

>

> A bit of a pain.

>

> > Does this conform to the C++ standard or is this another Microsoft

> > 'feature' ? I thought both versions of the declaration were

> > interchangable ?? Do I have to think again ??

>

> I think that if you add the std:: in the implementation (when there is

> no using), you should be conform. Not that that helps much if it

> doesn't work, and you cannot change compilers.

Well, as mentioned, std:: in the implementation doesn't do the trick, but I

don't actually have a problem with using 'using', it was just that I didn't

understand the error at all and probably could have spent hours staring at

the code and scratching my head if I hadn't known what change I had made

before triggered the error.

Good to know VC seems a bit picky there. (Hmmm, where do I write that down

for the next time I need it ?!? ;-)



Enjoy the weekend,



CU Steve





>

> --

> James Kanze mailto:jkanze@caicheuvreux.com

> Conseils en informatique orient   e objet/

> Beratung in objektorientierter Datenverarbeitung

>

> ---

> [ 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.jamesd.demon.co.uk/csc/faq.html ]

>



---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rogero@howzatt.demon.co.uk (Roger Orr)
Date: Fri, 15 Nov 2002 00:28:07 +0000 (UTC)
Raw View
steve_pole@yahoo.com ("Steve Pole") wrote in message news:<aqtk1k$den9r$1@ID-165031.news.dfncis.de>...

[snip]

> implementation:
>
> myStream::myStream ()
>  : ostream (new myStreamBuffer ())

Ah - this might be the problem.  Try:-

> myStream::myStream ()
>  : std::ostream (new myStreamBuffer ())

I suspect this will fix your problem.

Roger Orr
--
MVP in C++ at 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: steve_pole@yahoo.com ("Steve Pole")
Date: Thu, 14 Nov 2002 02:19:12 +0000 (UTC)
Raw View
Hi all,

This works:

[snip]

#include <iostream>
using std::ostream;

class myStream : public ostream
{
    ...
}

[snap]

But this doesn't:

[snip]

#include <iostream>

class myStream : public std::ostream
{
    ...
}

[snap]

implementation:

[snip]

myStream::myStream ()
 : ostream (new myStreamBuffer ())
{
}

[snap]

MSVC6 SP4 / W2K SP2 says 'error C2512: 'basic_ostream<char,struct
std::char_traits<char> >' : no appropriate default constructor available''
and 'error C2614: 'myStream' : illegal member initialization: 'ostream' is
not a base or member' at the init list of the constructor (which can be
pretty frustrating if you're not lucky enough to know what you changed...)

Does this conform to the C++ standard or is this another Microsoft 'feature'
? I thought both versions of the declaration were interchangable ?? Do I
have to think again ??

Thanks for your attention,

regards,

Steve



---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]