简体   繁体   English

C ++模板“不是类型”错误

[英]C++ template “is not a type” error

I have two templated classes. 我有两个模板化的类。 CCData and CCNode. CCData和CCNode。 The second (CCNode) declares two instances of the first (CCData) as part of its protected set of variables. 第二个(CCNode)声明第一个(CCData)的两个实例作为其受保护变量集的一部分。

In the cc_data.tpl.h file we have 在cc_data.tpl.h文件中

template<class T>
 class CCData
 {

 public:

  // Constructor. Allocates memory for the values. Initialise them to
  // zero
  CCData(const unsigned n_values, const unsigned n_history_values=1);

  .
  .
  .
  .

 };

And in the cc_node.tpl.h file we have 在cc_node.tpl.h文件中

template<class T, class DIM>
  class CCNode
 {

 public:

  // Constructor
  CCNode(const unsigned n_variables, const unsigned n_history_values);

  .
  .
  .
  .

 protected:

  // The number of variables stored in the node
  const unsigned N_variables;

  // The number of history values of the variable stored in the node
  const unsigned N_history_values;

  // Store the spatial position of the node
  CCData<double> X(DIM, N_history_values);

  // Store the values of the variables stored in the node
  CCData<T> U(N_variables, N_history_values);

 };

The cc_node.tpl.cpp file cc_node.tpl.cpp文件

template<class T, class DIM>
CCNode<T,DIM>::CCNode(const unsigned n_variables, const unsigned n_history_values)
 : N_variables(n_variables), N_history_values(n_history_values)
{ }

The problematic lines are in the cc_node.tpl.h file 有问题的行在cc_node.tpl.h文件中

  // Store the spatial position of the node
  CCData<double> X(DIM, N_history_values);

  // Store the values of the variables stored in the node
  CCData<T> U(N_variables, N_history_values);

where the compiler barks with 编译器吠叫的地方

cc_node.tpl.h:90:25: error: ‘N_history_values’ is not a type
cc_node.tpl.h:92:15: error: ‘N_variables’ is not a type
cc_node.tpl.h:92:28: error: ‘N_history_values’ is not a type

My gcc version 我的gcc版本

gcc version 5.4.0

with no fancy compilation flags other than 除以下以外,没有任何精美的编译标志

g++ -Wall -pedantic -O0 -g ...

first thing that you forgot is you passed a Type Name(DIM) as constructor arguments but constructor parameter should be value not a type name. 忘记的第一件事是您传递了类型名称(DIM)作为构造函数参数,但构造函数参数应为值而不是类型名称。 another problem is you defined a template function (constructor) in a .cpp file . 另一个问题是您在.cpp文件中定义了模板函数(构造函数)。 but you should define all template functions inside same header file to use. 但您应在同一头文件中定义所有模板函数以使用。 i Edited your code and it's works for me. 我编辑了您的代码,它对我有用。

 template<class T>
 class CCData
 {

 public:

  // Constructor. Allocates memory for the values. Initialise them to
  // zero
  CCData(const unsigned n_values, const unsigned n_history_values=1)
  {

  }

 };

 template<class T, class DIM>
 class CCNode
 {

 public:

  // Constructor
  CCNode(const unsigned n_variables, const unsigned n_history_values):
      N_variables(n_variables),
      N_history_values(n_history_values),
      X(n_variables,n_history_values),// pass CCData constructor arguments
      U(n_variables,n_history_values)// pass CCData constructor arguments
      {}

 protected:

  // The number of variables stored in the node
  const unsigned N_variables;

  // The number of history values of the variable stored in the node
  const unsigned N_history_values;

  // Store the spatial position of the node
  CCData<double> X;//(DIM, N_history_values); -> DIM is a type name not a value

  // Store the values of the variables stored in the node
  CCData<T> U;//(N_variables, N_history_values);

 };

You must put Constructor definition in same header file. 您必须将构造函数定义放在相同的头文件中。 because compiler needs template function definition when compiling. 因为编译器在编译时需要模板函数定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM