Why am I getting this errors when running this program where i tried to create 2 specialized templates to use int and double:

#include <iostream>
using namespace std;

template<>
int sum<int>(int* array, int start, int stop, int init = int())
{
	int* stp = (array + stop);
	int* str = (array + start);
	while(str != stp)
	{
		init += *str++;
	}
	return init;
}

template<>
double sum<double>(double* array, double start, double stop, double init = double())
{
	double* stp = (array + stop);
	double* str = (array + start);
	while(str != stp)
	{
		init += *str++;
	}
	return init;
}


int main()
{
	int a[] = {1,2,3,4,5,6,7,8,9};

	cout << sum<>(a, 2, 9);
}

Errors:

/5_Templates_in_Depth/6/main.cc:12: error: expected initializer before ‘<’ token
../5_Templates_in_Depth/6/main.cc: In function ‘int main()’:
../5_Templates_in_Depth/6/main.cc:40: error: ‘sum’ was not declared in this scope
../5_Templates_in_Depth/6/main.cc:40: error: expected primary-expression before ‘>’ token
../5_Templates_in_Depth/6/main.cc:40: warning: left-hand operand of comma has no effect
../5_Templates_in_Depth/6/main.cc:40: warning: right-hand operand of comma has no effect

Firstly, obviously you can see that you don't need any template specialization for this example. But assuming that this is a example, let us see what you did wrong.
It is just that sloppy practices that are allowed in non-template code are not allowed when dealing with templates.


(a) for a template function you must have a declaration .
(b) You cannot add a double to a pointer so start and stop should be int/unsigned int etc.
(c) you have to put the default initializer in the declaration.

// Put a declaration of the generic version [ALWAYS].
template<typename T> T sum(T*,int,int,T =T());

template<>
int sum<int>(int* array, int start, int stop, int init)
{
  int* stp = (array + stop);
  int* str = (array + start);
  while(str != stp)
    {
      init += *str++;
    }
  return init;
}

// If the type can be deduced then there is not need to write 
// the type
template<>
double sum(double* array, int start, int stop, double init)
{
  double* stp = (array + stop);
  double* str = (array + start);
  while(str != stp)
    {
      init += *str++;
    }
  return init;
}


int main()
{
  int a[] = {1,2,3,4,5,6,7,8,9};
  
  cout << sum(a, 2, 9,0)<<std::endl;
  cout << sum<>(a, 2, 9,0)<<std::endl;
  cout << sum<int>(a, 2, 9,0)<<std::endl;
}

Note that three ways to call sum. They are all allowed because the type can be deduced. If the template type was only the return type, then you would have to explicitly put the type.

Edited 6 Years Ago by StuXYZ: n/a

Comments
Helpful
template <class KeyType>
   Comparable<KeyType> *
   AvlNode<KeyType>::Insert(Comparable<KeyType> *   item,
                            AvlNode<KeyType>    * & root,
                            int                   & change)
   {
         // See if the tree is empty
      if (root == NULL) {
            // Insert new node here 
         root = new AvlNode<KeyType>(item);
         change =  HEIGHT_CHANGE;
         return  NULL;
      }

         // Initialize
      Comparable<KeyType> * found = NULL;
      int  increase = 0;

         // Compare items and determine which direction to search
      cmp_t  result = root->Compare(item->Key());
      dir_t  dir = (result == MIN_CMP) ? LEFT : RIGHT;

      if (result != EQ_CMP) {
            // Insert into "dir" subtree 
         found = Insert(item, root->mySubtree[dir], change);
         if (found)  return  found;   // already here - dont insert
         increase = result * change;  // set balance factor increment
      } else  {   // key already in tree at this node
         increase = HEIGHT_NOCHANGE;
         return  root->myData;
      }

      root->myBal += increase;    // update balance factor 

     // --------------------------------------------------------------------
     // re-balance if needed -- height of current tree increases only if its
     // subtree height increases and the current tree needs no rotation.
     // --------------------------------------------------------------------

      change =  (increase && root->myBal)
                     ? (1 - ReBalance(root))
                     : HEIGHT_NOCHANGE;
      return  NULL;
   }

this is the coding for avltree... errors occur when any value is passed in the function

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.