Error:Template as friend class with different arguement list

N

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>
template <class B,class C> class 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<B> myClass;
myClass E;
int j;
};
int main()
{
A<int,int> a;
return 0;
}
 
I

Ian Collins

Nike said:
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 C> class 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.
 
A

Alf P. Steinbach

* 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 <iostream> if you must. But you
don't need it for this code.

template <class B,class C> class 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;
 
N

Nike

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????
 
N

neelsmail

This works with MS VS 2003 --->

class B;
class C;
template <class B, class C> class 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 b> myClass;
myClass E;
int j;

};

int func()
{

A<int,int> a;
return 0;
}

Thanks,
Neel.
 
A

Alf P. Steinbach

* 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

Alf P. Steinbach

* (e-mail address removed):
This works with MS VS 2003 --->

class B;
class C;
template <class B, class C> class 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
the only instantiation used said:
template <typename b,typename c>
class A{
private:
typedef D<typename b> myClass;

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
 
N

neelsmail

* (e-mail address removed):









This doesn't make the template a friend, it makes the concrete class
A<B,C> a friend. In particular, A<int,int>, the only instantiation used
in the program, is not a friend of D.




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



--
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top