473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to have a template class as friend?

Dear all,

I would like to have a template class as friend of another class. If I write

class c
{ friend class t;
};

template <class Tclass t
{
};

then this is not allowed and on my g++ version 4.0.2. It says that
"t is not a template type". Apparently this is because of the friend
declaration, because if I comment it out, the code compiles without errors.
But I want t to be a friend of c no matter what the template argument
of t is. Can anyone tell me how to do this?

Many thanks,
Chris
Aug 11 '06 #1
4 3065
Chris Dams wrote:
I would like to have a template class as friend of another class. If
I write

class c
{ friend class t;
};

template <class Tclass t
{
};

then this is not allowed and on my g++ version 4.0.2. It says that
"t is not a template type". Apparently this is because of the friend
declaration, because if I comment it out, the code compiles without
errors. But I want t to be a friend of c no matter what the template
argument
of t is. Can anyone tell me how to do this?
class c
{
void foo(); // private
template<class Tfriend class t; // 't' is a template
};

template<class Tclass t {
public:
void bar(c& cc) { cc.foo(); }
};

int main() {
t<inttt;
c cc;
tt.bar(cc);
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 11 '06 #2
>
class c
{
void foo(); // private
template<class Tfriend class t; // 't' is a template
};

template<class Tclass t {
public:
void bar(c& cc) { cc.foo(); }
};

int main() {
t<inttt;
c cc;
tt.bar(cc);
}
I was just wondering that myself. Thanks, Victor.

(P.S., "tt.bar"? Haven't been to one of those in a while...)
Aug 11 '06 #3
Howard wrote:
[..]
(P.S., "tt.bar"? Haven't been to one of those in a while...)
I've never been to any of those! I just heard about them on t.vee() :)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 11 '06 #4
Dear Victor,

"Victor Bazarov" <v.********@comAcast.netwrites:
template<class Tfriend class t; // 't' is a template
Thanks!

I had instead been trying variations of

friend template<class Tclass t;

and that didn't work.

Best wishes,
Chris
Aug 12 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: Jim Avera | last post by:
On AIX, a template class containing a nested class which needs to be a friend provokes a warning: (W) A template dependent name that is a type must be qualified with "typename". and method...
4
by: StephQ | last post by:
According to: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.4 , if my understanding is correct, in template<typename T> class Foo { friend void func (const Foo<T>& foo); }; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.