Is it possible to use forward declaration with a template class?
Is possible to foward declare a template?
Testing says I couldn't, but maybe I didn't write the code right (which is what is included below)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class aFriend<int>;
class ABase
{
public: friend AFriend<int>;
protected: ABase(){}
};
template <class A>
class AFriend
{
public: AFriend(){ ABase Test; }
};
int main()
{
AFriend<int> Val;
return 0;
}
|
Thanks in advance.
Forward declaring looks just like the class definition (including the template<> part). So you'd forward declare it like so:
|
template <typename T> class AFriend;
|
Although you don't need to forward declare friends.
Obligatory link:
http://cplusplus.com/forum/articles/10627/#msg49682 see section 8
EDIT:
actually... maybe you do have to forward declare them when they're templates! Interesting!
Anyway the solution here:
1 2 3 4 5 6 7
|
template <class A> class AFriend; // <- forward declare template
class ABase
{
public: friend class AFriend<int>; // <- friend class. don't forget the class
protected: ABase(){}
};
|
Last edited on
Although you don't need to forward declare friends. |
It looks like vs2008 wants me to forward declare them. I used the same example as above except removed the template parts and I get this error:
"error c2433: 'AFriend' : 'friend' no permitted on data declarations"
While adding class AFriend; to forward declare it passes.
Nm, I added 'class' in front of AFriend and now compiles fine.
Thanks for your help!
Last edited on
Topic archived. No new replies allowed.