是否可以在模板类中使用模板化的类?

时间:2021-10-04 20:14:10
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 similar.

上面的代码可以吗?我希望能够做类似的事情。

Why I ask is that i get 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

4 个解决方案

#1


4  

My guess: you forward declared class M somewhere, and only declared it fully after the template instantiation.

我的猜测:你在某处转发声明的类M,并且只在模板实例化之后完全声明它。

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

我的提示:给出正式模板参数与实际名称不同的名称。 (即M级)

// template definition file
#include <list>

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

Example of a bad forward declaration, resulting in the mentioned 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; };

Example of proper declaration, not resulting in the error:

正确声明的示例,不会导致错误:

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

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

#2


2  

Yes. This is very common.

是。这很常见。

As xtofl mentioned, a forward declaration of your parameter would cause a problem at the time of template instantiation, which looks like what the error message is hinting at.

正如xtofl所提到的,参数的前向声明会在模板实例化时引起问题,这看起来就像错误消息所暗示的那样。

#3


1  

This is a very common usage.

这是一种非常常见的用法。

You should make sure that the class M that is specified as the template parameter is fully declared before the creation of the first instance of class C. Perhaps you are missing a header file include or perhaps this is a namespace issue.

您应该确保在创建类C的第一个实例之前完全声明了指定为模板参数的类M.也许您缺少头文件包含或者这可能是命名空间问题。

#4


0  

Yes.

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

它被STL用于分配器和迭代器之类的东西。

It looks like you are running into some other issue. Perhaps you are missing a template on an out of line method body definition that was first declared in the ... you elided?

看起来你遇到了其他一些问题。也许你错过了一个最终在...中省略的方法体定义的模板?

#1


4  

My guess: you forward declared class M somewhere, and only declared it fully after the template instantiation.

我的猜测:你在某处转发声明的类M,并且只在模板实例化之后完全声明它。

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

我的提示:给出正式模板参数与实际名称不同的名称。 (即M级)

// template definition file
#include <list>

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

Example of a bad forward declaration, resulting in the mentioned 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; };

Example of proper declaration, not resulting in the error:

正确声明的示例,不会导致错误:

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

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

#2


2  

Yes. This is very common.

是。这很常见。

As xtofl mentioned, a forward declaration of your parameter would cause a problem at the time of template instantiation, which looks like what the error message is hinting at.

正如xtofl所提到的,参数的前向声明会在模板实例化时引起问题,这看起来就像错误消息所暗示的那样。

#3


1  

This is a very common usage.

这是一种非常常见的用法。

You should make sure that the class M that is specified as the template parameter is fully declared before the creation of the first instance of class C. Perhaps you are missing a header file include or perhaps this is a namespace issue.

您应该确保在创建类C的第一个实例之前完全声明了指定为模板参数的类M.也许您缺少头文件包含或者这可能是命名空间问题。

#4


0  

Yes.

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

它被STL用于分配器和迭代器之类的东西。

It looks like you are running into some other issue. Perhaps you are missing a template on an out of line method body definition that was first declared in the ... you elided?

看起来你遇到了其他一些问题。也许你错过了一个最终在...中省略的方法体定义的模板?

相关文章