G
Gianni Mariani
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>:oThing(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
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>:oThing(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