On 10/27/2010 10:25 AM, Andrea Crotti wrote:
You're confusing the declaration and definition. A definition
(the part that you have later in the code) is a declaration as
well, but in this particular case multiple declarations don't
hurt, since they are all the same.
What you did is pretty much what you can do, first declare and
then define.
Now, there is another problem. You aren't supposed to use an
incomplete class as the template argument. Some compilers let
you slide on that, and some don't like it.
Probably just a question of wording, but as stated, what you've
said is false. There's absolutely no restriction in the
language with regards to instantiating a template over an
incomplete type, as long as there is nothing in the
instantiation which requires the type to be complete. Whether
this is guaranteed to be the case or not is up to the author of
the template.
What you doubtlessly meant is that it is undefined behavior to
instantiate a template in the standard library using an
incomplete type, because the standard library imposes this
requirement. (In other words, the standard library allows the
implementor to assume that the type is complete everywhere in
the template.) Typically, you can get away with it sometimes,
but not always; some of the better implementations add special
code (static constraints checking) to ensure that you will get
a compiler error instead of undefined behavior (but in practice,
in all but a very few cases, the "undefined behavior" will be
that the code will either work, or it won't compile).
With regards to the OP's problem, the only real solution I see
is a vector of pointers, a pointer to an incomplete type is
a complete type, e.g.:
class Toto; // incomplete type.
Toto* p; // p has a complete type.