模板实例做模板参数

来源:互联网 发布:matlab矩阵行列式的值 编辑:程序博客网 时间:2024/04/23 18:23

未专用化的类 模板 不能用作 模板 变量,该变量属于 模板 参数“_Ty”,应为 real 类型

今天做一个程序,在编译的时候遇到如上所述的问题。

代码如下:

 

C++语言: Codee#15880
template<typename TYPE>
struct Node
{
    TYPE info;
    TYPE newValue;        //保存该节点节点新一轮的质心
    TYPE oldValue;        //保存该节点上一轮的质心
};

bool K_Average(vector<Node> set)
{
  return true;
}

问题出在了一下这行:
bool K_Average(vector<Node> set)

错误提示:
 error C3203: “Node”: 未专用化的类 模板 不能用作 模板 变量,该变量属于 模板 参数“_Ty”,应为 real 类型
这里Node本身是一个模板,在这里又作为另一个模板vector的模板变量,导致错误。

先来看看MSDN 中是如何描述模板作为模板变量的:

Template parameters can themselves be templates. This construct means that the argument must itself be a template, not a class constructed from template. In the following example, the name A of the template parameter for a template template parameter can be omitted, because there is no way that it can be used.

 

 

C++语言: Codee#15881
// template_specifications3.cpp
#include 

template <class T> struct str1
{
   T t;
};

template <template<class A> class T> struct str2
{
    T<int> t;
};

int main()
{
    str2<str1> mystr2;
    mystr2.t.t = 5;
    printf_s("%d\n", mystr2.t.t);
}
也就是说将一个模板作为另一个模板的模板参数时候必须声明其是一个模板,而不能直接使用它作为一种新类型。

修改后代码如下:
C++语言: Codee#15882
template<typename TYPE>
struct Node               //定义信息节点模板结构体
{
    TYPE info;
    TYPE newValue;        //保存该节点节点新一轮的质心
    TYPE oldValue;        //保存该节点上一轮的质心
};

template <typename TYPE>
bool K_Average(vector<Node<TYPE> > set)
{
return true;
}
原创粉丝点击