Is it possible to have templated classes in a templating class?

template <class M, class A> class C { std::list<M> m_List; ... }

      

Is the above code possible? I would like to be able to do something like this.

Why am I asking that I am getting the following error:

Error 1 error C2079: 'std::_List_nod<_Ty,_Alloc>::_Node::_Myval' uses undefined class 'M'   C:\Program Files\Microsoft Visual Studio 9.0\VC\include\list    41

      

+1


source to share


4 answers


My guess: you posted your declared M class somewhere and only declared it in full after you create the template.

My hint: give your formal template arguments a different name than the actual one. (i.e. class M)

// template definition file
#include <list>

template< class aM, class aT >
class C {
    std::list<M> m_List;
    ...
};

      

An example of an unsuccessful declaration leading to the specified error:



// bad template usage file causing the aforementioned error
class M;
...
C<M,OtherClass> c; // this would result in your error

class M { double data; };

      

An example of a correct declaration that does not result in an error:

// better template usage file
class M { double data; }; // or #include the class header
...

C<M,OtherClass> c; // this would have to compile

      

+4


source


Yes. This is very common.



As xxtl mentioned, directly declaring your parameter will cause a problem during template instantiation, similar to what the error message hints at.

+2


source


This is a very common use.

You need to make sure that the M class specified as a template parameter is fully declared before the first instance of the C class is instantiated. You may be missing a header file, or it may be a namespace issue.

+1


source


Yes.

It is used a lot by the STL for things like allocators and iterators.

It looks like you are facing some other problem. You may be missing a pattern for defining an off-line method body that was first declared in ... did you remove?

0


source







All Articles