C++ template "is not a type" error

I have two templated classes. CCData and CCNode. The second (CCNode) declares two instances of the first (CCData) as part of its protected set of variables.

In the cc_data.tpl.h file we have

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

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

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

  // 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 version 5.4.0

with no fancy compilation flags other than

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

Upvotes: 0

Views: 3774

Answers (1)

amir alizadeh
amir alizadeh

Reputation: 28

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. another problem is you defined a template function (constructor) in a .cpp file . 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.

Upvotes: 1

Related Questions