469,322 Members | 1,622 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 2881

"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by fabio de francesco | last post: by
1 post views Thread by Tom McCallum | last post: by
9 posts views Thread by Maciej | last post: by
2 posts views Thread by Niklas Norrthon | last post: by
25 posts views Thread by Ted | last post: by
9 posts views Thread by Klaas Vantournhout | last post: by
1 post views Thread by deepaks85 | last post: by
4 posts views Thread by aaragon | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.