You are here:

C++/Template Function

Advertisement


Question
Hello,
When I use the refrence method to pass the array argument (with variable elements) to template function, it gives following error :

(In Microsoft Visual C++ 6.0 :)
---------
=> CODE :
---------
template < class AT, int n > unsigned long int count ( AT (& array) [n], const AT value )
{
  unsigned long int res = 0;

  for (unsigned long int i=0; i<n; i++)
     if ( array[i] == value )
        res++;
  
  return res;
}
----------
=> ERROR : error C2265: '<Unknown>' : reference to a zero-sized array is illegal
----------

=> Please tell me what is the problem, that cause this error ?

Answer
because you want to have the following comparison:

if ( array[i] == value )

you should have the parameter defined like:

template < class AT, int n > unsigned long int count ( AT array[n], const AT value )

or

template < class AT, int n > unsigned long int count ( AT *array, const AT value )  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.