473,386 Members | 1,873 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,386 software developers and data experts.

templates and friends


I have 2 distinct template classes which co-operate, hence are friends.
However, I can't seem to figure out what syntax to use to make this work.

What is the right(tm) way to write a friend class here ?

Here is the code:

template <typename A>
class Y;

template <typename A>
class X
{
friend class Y<A>;
private:

A m_a;

public:

X( A i_val );

template<typename B> void DoThing( Y<B> & b )
{
m_a = b.m_a;
}

};

template <typename A>
class Y
{
friend class X<A>;
private:

A m_a;

public:

Y( A i_val );

template<typename B> void DoOtherThing( Y<B> & b )
{
m_a = b.m_a;
}

};

int main()
{

Y<int> yi( 1 );
X<short> xs( 2 );

xs.DoThing( yi );

}

Here is the error:

g++ -c -o testfriend.o testfriend.cpp
testfriend.cpp: In member function `void X<A>::DoThing(Y<B>&) [with B =
int, A
= short int]':
testfriend.cpp:52: instantiated from here
testfriend.cpp:31: error: `int Y<int>::m_a' is private
testfriend.cpp:20: error: within this context
make: *** [testfriend.o] Error 1

Jul 19 '05 #1
1 3147

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:be********@dispatch.concentric.net...

I have 2 distinct template classes which co-operate, hence are friends.
However, I can't seem to figure out what syntax to use to make this work.
What is the right(tm) way to write a friend class here ?

Here is the code:

template <typename A>
class Y;

template <typename A>
class X
{
friend class Y<A>;
private:

A m_a;

public:

X( A i_val );

template<typename B> void DoThing( Y<B> & b )
{
m_a = b.m_a;
}

};

template <typename A>
class Y
{
friend class X<A>;
private:

A m_a;

public:

Y( A i_val );

template<typename B> void DoOtherThing( Y<B> & b )
{
m_a = b.m_a;
}

};

int main()
{

Y<int> yi( 1 );
X<short> xs( 2 );

xs.DoThing( yi );

}

Here is the error:

g++ -c -o testfriend.o testfriend.cpp
testfriend.cpp: In member function `void X<A>::DoThing(Y<B>&) [with B =
int, A
= short int]':
testfriend.cpp:52: instantiated from here
testfriend.cpp:31: error: `int Y<int>::m_a' is private
testfriend.cpp:20: error: within this context
make: *** [testfriend.o] Error 1


You have to distinguish between

1) All template instances are friends of each other, e.g. X<int> is a friend
of Y<double>

2) Only templates instantiated with the same type are friend of each other,
e.g. X<int> is a friend of Y<int> but not of Y<double>.

You want case 1, but the code you've written is for case 2. Try this

template <typename A>
class Y;

template <typename A>
class X
{
template <typename B>
friend class Y;

etc.

john
Jul 19 '05 #2

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

Similar topics

7
by: fabio de francesco | last post by:
Hello, I'm just joking with the Subject, but I really don't know how to make a synthesis of two questions about some code I'm trying to write. In the following I post this little code, with...
1
by: Tom McCallum | last post by:
Hi, Can someone please tell me the correct syntax (if its possible of course) to specify an output stream operator for a templated class so that I dont need to write the same function for all...
9
by: Maciej | last post by:
Hi, I tried to build windl project from the book 'The Art of C++" in VS.NET 7 and it fails. I attached all required .lib files. I got the following error messages from the linker: windl error...
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...
2
by: Niklas Norrthon | last post by:
I want to share a technique I recently have found to be useful to get around some obstacles that data protection can raise. Consider the following class: // foo.h #ifndef H_FOO #define H_FOO...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
9
by: Klaas Vantournhout | last post by:
Hi all, I have a question about friends functions of a template class. To make it simple, I would like to do something like this. Assume that I have a class foo with template T ...
1
by: deepaks85 | last post by:
Hi Friends, I am looking for some good web banners or some good web design templates. I am a web designer and developer and I want some banners and templates for my own website so that I can make...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.