Template specialization.

Hi,

I'm currently working my way through "C++ Primer Plus 5th-Edition" by Stephen Prata.
However, I'm stuck on Question 5, chapter 8. I can't seem to get the specialized
function to work. When i compile the code (in Code::blocks), i get the following errors:


5-make-a-template_and_specialization.cpp|16|error: template-id ‘maxn<char*>’
for ‘char* maxn(const char*, int)’ does not match any template declaration|

5-make-a-template_and_specialization.cpp|53|error: template-id ‘maxn<char*>’
for ‘char* maxn(const char*, int)’ does not match any template declaration|
||=== Build finished: 2 errors, 0 warnings ===|


Can anyone tell me what it is i'm doing wrong and how i can fix it?
Many thanks.
Regards, EightBit.


Here is the Question:

Write a template function maxn() that takes as its arguments an array of items of type T
and an integer representing the number of elements in the array and that returns the
largest item in the array. Test it in a program that uses the function template with an
array of six int value and an array of four double values. The program should also
include a specialization that takes an array of pointers-to-char as an argument and the
number of pointers as a second argument and that returns the address of the longest
string. If multiple strings are tied for having the longest length, the function should
return the address of the first one tied for longest. Test the specialization with an array of
five string pointers.


//
#include <iostream>
#include <cstring> // for the strlen() function

template <typename T>
T maxn(const T * ar, int n);
template <> char * maxn<char*>(const char * ar, int n); // line 16.

int main()
{
using namespace std;
const char * cities[5] = // array of pointers to 5 strings
{
"Gribble City",
"Gribbletown",
"New Gribble",
"San Gribble",
"Gribble Vista"
};
double ar1[4] = {2.2, 4.4, 18.18, 23.23};
int ar2[6] = {17, 67, 45, 33, 29, 77};
double max_double = maxn(ar1, 4);
int max_int = maxn(ar2, 6);
cout << "Largest double is: " << max_double << endl;
cout << "Largest int is: " << max_int << endl;
const char * ptr = maxn(cities, 5);
cout << "The largest string is: " << ptr << endl;

cout << "\nDone!\n";
cin.get();
return 0;
}
template <class T>
T maxn(const T * ar, int n)
{
T biggest = ar[0];
for (int i = 1; i < n; i++)
{
if (ar[i] > biggest)
biggest = ar[i];
}
return biggest;
}
template <> char * maxn<char*>(const char * ar, int n) // line 53
{
char * temp = str[0];
for (int i = 1; i < n; i++)
{
if (strlen(str[i]) > strlen(temp))
char * temp = str[i];
}
return temp;
}
template <> char * maxn<char*>(const char * ar, int n) // line 53
should probably be
char* maxn(const char* str[], int n) // line 53


and template <> char * maxn<char*>(const char * ar, int n); // line 16. should also be changed like above

also, when calling maxn for double and int, you cant call it like a normal function, its a template, you need to specify the type:
1
2
double max_double = maxn<double>(ar1, 4);
int max_int = maxn<int>(ar2, 6);
Last edited on
Hi skillless,

Thank you for your response.

> template <> char * maxn<char*>(const char * ar, int n) // line 53
> should probably be
> char* maxn(const char* str[], int n) // line 53

> and template <> char * maxn<char*>(const char * ar, int n); // line 16. should also be changed
> like above

does your solution qualify as a specialization? it looks to me like your just overloading maxn().

> also, when calling maxn for double and int, you cant call it like a normal function, its a
> template, you need to specify the type:

> double max_double = maxn<double>(ar1, 4);
> int max_int = maxn<int>(ar2, 6);

The book did not metion this and calls template functions like regular functions. also,
I ran the program with just the maxn for double and int template, and it worked fine.

Regards, EightBit.
Sorry, im a noob at template specialization, yeah, i overloaded it

you were right using template<> char* maxn<char*>

so im guessing it should be; template<> char* maxn<char*>(const char* str[], int n)

i suppose you can call template functions without passing the template parameter, but im not sure if thats always going to work
Last edited on
Skillless wrote:
i suppose you can call template functions without passing the template parameter, but im not sure if thats always going to work

Templates will attempt to infer their parameters based on the variables you pass. If it cannot infer them, it will generate a compiler error and you will need to specify them explicitly.
Topic archived. No new replies allowed.