R
Ralf Goertz
Hi,
while I was redesigning some of my programs to have them resemble cool
C++ code my compiler gave me a hard time. For instance, I use a
templated base class and two derived classes. These need to know about
each others private internals so that I decided to make them friends of
each other. But in order to do that I need a forward declaration of one
of the classes. I couldn't find out how to do that correctly. The code
below doesn't compile, g++ says "error: template argument required for
'struct Derived2'". But if I do it like in (**) it says "error:
'Derived2' is not a template". I tried almost all word permutations of
friend template class Derived2<T>
for line (*) but I had no success. So I ended up making both classes
public which is bad. What is the correct way of doing this?
template <class ID_TYPE> class Base
{
ID_TYPE id;
};
template <class T> class Derived1: public Base<T>
{
friend class Derived2; // (*)
};
template <class T> class Derived2: public Base<T>
{
friend class Derived1<T>; (**)
};
int main()
{
Derived1<int> d1;
return 0;
}
while I was redesigning some of my programs to have them resemble cool
C++ code my compiler gave me a hard time. For instance, I use a
templated base class and two derived classes. These need to know about
each others private internals so that I decided to make them friends of
each other. But in order to do that I need a forward declaration of one
of the classes. I couldn't find out how to do that correctly. The code
below doesn't compile, g++ says "error: template argument required for
'struct Derived2'". But if I do it like in (**) it says "error:
'Derived2' is not a template". I tried almost all word permutations of
friend template class Derived2<T>
for line (*) but I had no success. So I ended up making both classes
public which is bad. What is the correct way of doing this?
template <class ID_TYPE> class Base
{
ID_TYPE id;
};
template <class T> class Derived1: public Base<T>
{
friend class Derived2; // (*)
};
template <class T> class Derived2: public Base<T>
{
friend class Derived1<T>; (**)
};
int main()
{
Derived1<int> d1;
return 0;
}