470,591 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Error:Template as friend class with different arguement list

The following code doesn't compile.
I have a requirement in my project to declare one template as a friend
function to another..
Can anyone suggest a way????
#include <iostream.h>
template <class B,class Cclass A; //forward declaration
template <class DBT_>
class D{
public:
friend class A<B,C>; //Error as B and C are not known as template
parameters
private :
DBT_ f;

};
template <class B,class C>
class A{
private:
typedef D<BmyClass;
myClass E;
int j;
};
int main()
{
A<int,inta;
return 0;
}

May 22 '07 #1
7 1455
Nike wrote:
The following code doesn't compile.
I have a requirement in my project to declare one template as a friend
function to another..
Can anyone suggest a way????
#include <iostream.h>
template <class B,class Cclass A; //forward declaration
template <class DBT_>
class D{
public:
friend class A<B,C>;
You have to be explicit here, B and C are unknown types.

--
Ian Collins.
May 22 '07 #2
* Nike:
The following code doesn't compile.
I have a requirement in my project to declare one template as a friend
function to another..
Can anyone suggest a way????
#include <iostream.h>
This is not a standard C++ header. Use <iostreamif you must. But you
don't need it for this code.

template <class B,class Cclass A; //forward declaration
template <class DBT_>
Reserve all uppercase names (except sometimes single letter names like
T, which have established convention) for macros. See this group's FAQ
as well as Bjarne Stroustrup's FAQ.

class D{
public:
friend class A<B,C>; //Error as B and C are not known
template< class B, class C friend class A;

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #3
Thankx for ur suggestions Alf..
I just changed the name of the class what I have in my project to this
format..
As far as this question goes, the question I have is ,
"Is it not @ all possible to have kind of declarations as I have
suggested in the code????

May 22 '07 #4
This works with MS VS 2003 --->

class B;
class C;
template <class B, class Cclass A; //forward declaration

template <class DBT_>
class D {
public:
friend class A<B,C>; //Error as B and C are not known as
template parameters
private :
DBT_ f;
};
template <typename b,typename c>
class A{
private:
typedef D<typename bmyClass;
myClass E;
int j;

};

int func()
{

A<int,inta;
return 0;
}

Thanks,
Neel.

May 22 '07 #5
* Nike:
Thankx for ur suggestions Alf..
I just changed the name of the class what I have in my project to this
format..
I think you mean you used the syntax I exemplified.

As far as this question goes, the question I have is ,
"Is it not @ all possible to have kind of declarations as I have
suggested in the code????
That depends on what you're trying to achieve. You can use that syntax
for declaring a concrete instantiation of the A template as friend. To
declare the template itself as friend, use the syntax I exemplified.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #6
* ne*******@rediffmail.com:
This works with MS VS 2003 --->

class B;
class C;
template <class B, class Cclass A; //forward declaration

template <class DBT_>
class D {
public:
friend class A<B,C>; //Error as B and C are not known as
template parameters
private :
DBT_ f;
};
This doesn't make the template a friend, it makes the concrete class
A<B,Ca friend. In particular, A<int,int>, the only instantiation used
in the program, is not a friend of D.
>

template <typename b,typename c>
class A{
private:
typedef D<typename bmyClass;
This is invalid syntax. Use just "b", not "typename b". Even though
MSVC may allow it, "typename" is not a kind spice that you can sprinkle
liberally everywhere in the hope of satisfying the compiler.

Use "typename" to inform the compiler that a dependent name denotes a type.

A dependent name is a name which definition depends on a template
parameter, e.g. Foo<b>::Type.

myClass E;
int j;

};

int func()
{

A<int,inta;
return 0;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 22 '07 #7
On May 22, 11:49 am, "Alf P. Steinbach" <a...@start.nowrote:
* neelsm...@rediffmail.com:


This works with MS VS 2003 --->
class B;
class C;
template <class B, class Cclass A; //forward declaration
template <class DBT_>
class D {
public:
friend class A<B,C>; //Error as B and C are not known as
template parameters
private :
DBT_ f;
};

This doesn't make the template a friend, it makes the concrete class
A<B,Ca friend. In particular, A<int,int>, the only instantiation used
in the program, is not a friend of D.
template <typename b,typename c>
class A{
private:
typedef D<typename bmyClass;

This is invalid syntax. Use just "b", not "typename b". Even though
MSVC may allow it, "typename" is not a kind spice that you can sprinkle
liberally everywhere in the hope of satisfying the compiler.

Use "typename" to inform the compiler that a dependent name denotes a type.

A dependent name is a name which definition depends on a template
parameter, e.g. Foo<b>::Type.
myClass E;
int j;
};
int func()
{
A<int,inta;
return 0;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?- Hide quoted text -

- Show quoted text -
Thanks Alf, should have paid more attention !

Thanks,
Neel.

May 22 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Steve Franks | last post: by
reply views Thread by guy | last post: by
5 posts views Thread by crystalattice | last post: by
11 posts views Thread by kimiraikkonen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.