I wrote a template class which is giving compilation error
template
class Entity
{
string EntityName;
int EntitySize;
Entity
Read the error message more closely. The "too complex" thing is not a recursive function, it's a recursive type or function dependency. The type Entity
depends on the type Entity
, recursively. When the compiler tries to generate the code for Entity
, it will have to figure out Entity
(in order to implement the pPrev
and pNext
members), which means it will have to figure out Entity
, etc. - infinitely. That isn't allowed.
But that's just how the compiler knows something is wrong. It doesn't know what is wrong, because it can't think about how to program. (If it could, it would just write your program for you.)
The logical error is that Entity
means "an object which is an Entity with template type pointer-to-T". What you really wanted, in order to make a linked list, is "a pointer to an object which is an Entity with template type T". That is spelled Entity
, with the * outside the angle brackets.
But the real problem is that you are trying to create your own linked list. Don't do that. Use the standard library containers. If you're smart enough to use std::string
, you should be smart enough to use the containers (std::vector
, std::list
, etc. - in a sense, std::string
is a container, too, albeit a very special-purpose one) too.