Topic: Visual C++ template friend bugs (was templates)


Author: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/03/18
Raw View
Marcin Kasperski wrote in message <36F0ACEF.C6C341FF@softax.com.pl>...
[snip]
>Visual C++ seems to have bugs with respect to friend templates. The
>following code (I tested on 6.0 SP2) doesn't compile correctly because
>ostream is typedef to some template (if I switch to #include
><iostream.h> - where ostream is a class, the code compiles and run
>correctly):

You are correct w.r.t. "bugs" but your
diagnosis is a little off.  I have cut
irrelevant portions of following code.

>#include <iostream>
>using namespace std;
>
>class test
>{
>public:
>    friend ostream &operator<<(ostream & out, test &t);
>private:
>    int v;
>};
>
>ostream &operator<<(ostream & out, test &t)
>{
>    out << t.v;
>}
>
>Compilation output:
>
>Compiling...
>test.cpp
>C:\TEMP\test.cpp(18) : error C2248: 'v' : cannot access private member
>declared in class 'test'
>        C:\TEMP\test.cpp(13) : see declaration of 'v'
>C:\TEMP\test.cpp(24) : error C2593: 'operator <<' is ambiguous
>Error executing cl.exe.

Your code exposes a bug in VC6.  You have 3 choices
to get around it.  (1) Stop writing "using namespace std;"
and start using the std:: qualifier and "using std::XXX;".
(2) Declare the friend function outside the class body
before declaring it to be a friend. (3) Get a new front-end
for your compiler.  See Microsoft's knowledge-base article
http://support.microsoft.com/support/kb/articles/q192/5/39.asp

Choice 1 is a good idea for other reasons.  I can vouch
for the effectiveness of all 3.  If you are willing to spend
a few minutes, getting a new front-end is the cleanest.

--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/03/19
Raw View
Larry Brasfield wrote:
>
> (3) Get a new front-end
> for your compiler.
> If you are willing to spend
> a few minutes, getting a new front-end is the cleanest.
>

You are saying that I can substitute the front-end of VC++ with
something else? Really? How do you do that?

--
Best regards,
Biju Thomas


[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Larry Brasfield" <larrybr@seanet.com>
Date: 1999/03/19
Raw View
Biju Thomas <bijuthom@ibm.net> wrote in message news:36F17703.9F904A88@ibm.net...
>
> Larry Brasfield wrote:
> >
> > (3) Get a new front-end
> > for your compiler.
[Restoring the next sentence trimmed by Biju's quote:]
See Microsoft's knowledge-base article
http://support.microsoft.com/support/kb/articles/q192/5/39.asp

> > If you are willing to spend
> > a few minutes, getting a new front-end is the cleanest.
>
> You are saying that I can substitute the front-end of VC++ with
> something else? Really? How do you do that?

Really.  Just follow the instructions in the article I cited.

I believe any more on this matter is clearly off-topic, so I
suggest followups directly to microsoft.public.vc.language
rather than further diluting this forum.

--
--Larry Brasfield
Above opinions may be mine alone.
X-Replace-Address
(Humans may reply at unundered larry_br@sea_net.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Marcin Kasperski <Marcin.Kasperski@softax.com.pl>
Date: 1999/03/18
Raw View
Ben Jones wrote:
>=20
> Thanks for your suggestion Siemel...
>=20
> >   template <class T> friend TemplateClass<T>;
>=20
> I tried this, but with visual C++ I get:
> error C2059: syntax error : '<end Parse>'
> error C2143: syntax error : missing ';' before '}'
> error C2238: unexpected token(s) preceding ';'
>=20
> Does it work for you? -- in which case, what compiler are you using? Is=
 it
> part of the standard?
>=20

Visual C++ seems to have bugs with respect to friend templates. The
following code (I tested on 6.0 SP2) doesn't compile correctly because
ostream is typedef to some template (if I switch to #include
<iostream.h> - where ostream is a class, the code compiles and run
correctly):

#include <iostream>
using namespace std;

class test
{
public:
    test()
    {
        v =3D 1;
    }
    friend ostream &operator<<(ostream & out, test &t);
private:
    int v;
};

ostream &operator<<(ostream & out, test &t)
{
    out << t.v;
}

int main()
{
    test t;
    cout << t << endl;
    return 0;
}

Compilation output:

Compiling...
test.cpp
C:\TEMP\test.cpp(18) : error C2248: 'v' : cannot access private member
declared in class 'test'
        C:\TEMP\test.cpp(13) : see declaration of 'v'
C:\TEMP\test.cpp(24) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

-- Marcin Kasperski     Marcin.Kasperski<at>softax.com.pl
--                      marckasp<at>friko6.onet.pl
-- Moje pogl=B1dy s=B1 moimi pogl=B1dami, nikogo poza mn=B1 nie reprezent=
uj=B1.
-- (My opinions are just my opinions.)
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html              ]