Re: Why won't this compile
- From: "Tom Widmer [VC++ MVP]" <tom_usenet@xxxxxxxxxxx>
- Date: Tue, 13 Dec 2005 11:30:26 +0000
Carl Daniel [VC++ MVP] wrote:
"Larry" <Larry@xxxxxxxxxxxxx> wrote in message news:F0A979C1-E43D-4632-8BB7-B651D1D32BD6@xxxxxxxxxxxxxxxx
Hi there,
Can someone explain why the following code won't compile in both VC7 and VC8
(yields an error that the default constructor I'm invoking is private). I've
made the calling function a friend however so it shouldn't happen as far as I
know. Thanks.
You haven't made the function a friend. Rather, you've made a non-template function name Func a friend.
You want this:
template <class T> class CTest { CTest() { }
template <class U> friend void Func(T);
That declares that the template function template <class U> void Func(T); is a friend, not the function template <class U> void Func(U); which is presumably closer to what is desired. So one fix is to write: template <class U> friend void Func(U); //declare friendship to all specializations.
};
template <class T> void Func(T) { CTest<T> Var;
I don't think that this function is a friend of CTest<T>, so that shouldn't compile.
}
int main() { Func(3);
This instantiation means that template <class U> void Func(int); is a friend, but that function doesn't have a definition.
return 0; }
In order to only grant friendship to the relevant specialization of Func, this is the required code:
template <class T> void Func(T);
template <class T>
class CTest
{
CTest()
{
}friend void Func<T>(T); };
//rest the same
Tom .
- Follow-Ups:
- Re: Why won't this compile
- From: Carl Daniel [VC++ MVP]
- Re: Why won't this compile
- References:
- Re: Why won't this compile
- From: Carl Daniel [VC++ MVP]
- Re: Why won't this compile
- Prev by Date: Re: how to transfer a DWORD from a function?
- Next by Date: Re: DirectShow in platform SDK
- Previous by thread: Re: Why won't this compile
- Next by thread: Re: Why won't this compile
- Index(es):
Relevant Pages
|