Topic: Repost of: template friend functions


Author: "Oved Itzhak" <oved@techie.com>
Date: 12 Sep 00 21:16:52 GMT
Raw View
Have you tried to put "inline" in the function definition?

"Anthony Williams" <anthony_w.geo@yahoo.com> wrote in message
news:8pi4bd$d807e$1@ID-49767.news.cis.dfn.de...
> I posted this about a week ago, and it didn't show up on Deja, so I'm
going
> to
> try again.
> - ---
> I have a class template, and I want to declare a friend function that
> performs
> some operation on this class. The problem is, I want this friend
> function to be
> a template
>
> template<typename T>
> class MyClass
> {
>   template<typename U>
>   friend void func(MyClass<T> mc,U data);
> };
>
> How can I define this function outside the class template?
>
> I tried:
>
> template<typename T>
> template<typename U>
> void func(MyClass<T> mc,U data)
> {}
>
> and
>
> template<typename T,typename U>
> void func(MyClass<T> mc,U data)
> {}
>
> both of which compiled OK, but didn't link, as the compiler didn't
> match these
> template definitions to that declared in the friend declaration.

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




Author: "Anthony Williams" <anthony_w.geo@yahoo.com>
Date: 12 Sep 00 08:07:36 GMT
Raw View
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I posted this about a week ago, and it didn't show up on Deja, so I'm going
to
try again.
- ---
I have a class template, and I want to declare a friend function that
performs
some operation on this class. The problem is, I want this friend
function to be
a template

template<typename T>
class MyClass
{
  template<typename U>
  friend void func(MyClass<T> mc,U data);
};

How can I define this function outside the class template?

I tried:

template<typename T>
template<typename U>
void func(MyClass<T> mc,U data)
{}

and

template<typename T,typename U>
void func(MyClass<T> mc,U data)
{}

both of which compiled OK, but didn't link, as the compiler didn't
match these
template definitions to that declared in the friend declaration.

Help is much appreciated.

Anthony

- --
alink@anthonyw.cjb.net -- Questions relating to ALINK
anthony@anthonyw.cjb.net  -- Non-ALINK questions
http://anthonyw.cjb.net/ -- ALINK home page
PGP Fingerprint:
0E2D D32A 8732 DC31 804C  D435 9BF0 F8FE 1C1B 9AD5
PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.1 for non-commercial use <http://www.pgp.com>
Comment: PGP Key at: http://i3.yimg.com/3/c7e5ee24/g/68fc2307.asc

iQA/AwUBObyUY5vw+P4cG5rVEQLjOQCgowp6pmj6fo3plOIMKej2hbUw100AniyJ
p8toa2hbndejMO3JlJqaVniO
=kFij
-----END PGP SIGNATURE-----




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