Can I use forward declaration for template class?
I try:
template<class que_type>
class que;
int main(){
que<int> mydeque;
return 0;
}
template<class que_type>
class que {};
I get:
error: aggregate 'que<int> mydeque' has incomplete type and cannot be defined.
Forward declaration of a class should have complete arguments list specified. This would enable the compiler to know it’s type.
When a type is forward declared, all the compiler knows about the type is that it exists; it knows nothing about its size, members, or methods and hence it is called an Incomplete type. Therefore, you cannot use the type to declare a member, or a base class, since the compiler would need to know the layout of the type.
You can:
1. Declare a member pointer or a reference to the incomplete type.
2. Declare functions or methods which accepts/return incomplete types.
3. Define functions or methods which accepts/return pointers/references to the incomplete type.
Note that, In all the above cases, the compiler does not need to know the exact layout of the type & hence compilation can be passed.
Example of 1: