Template specialization error

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
}
The following works for me.

1
2
3
4
5
template <typename T>
T maxn( const T * ar, int n);

template <> 
 char* maxn <char*>( char * const *  ar, int n);     



-----------------------

1
2
3
4
5
6
7
8
9
10
11
12
template <> 
char*  maxn <char *>(char * const * ar, int n)   
{
     char * temp = ar[0];
    for (int i = 1; i < n; i++)
    {
         if (strlen(ar[i]) > strlen(temp))
            /* char * */ temp = ar[i];  //note the change here as well - local variables and all that..
    }
    return temp;
}
Last edited on
line 43 (commented as line 53) is wrong.

It is not a specialization of the template if you look carefully.
According to the return type, T must be of type char*. But
according to the "ar" parameter, T must be of type char.
C++ Primer Series are great books and the Fifth edition by Prata is one that's on my shelf. But if you allow me a suggestion: If you are to make all the way through a book now i'd recommend one:
PROGRAMMING: Principles and Practice using C++ by Bjarne Stroustrupp. It's his very first tutorial for beginners printed last year. As far as I know C++ Primer Plus is from 2005. Try it!
Hi,

Thankyou guestgulkan & jsmith for your responses.

jsmith, I don't really follow your solution. (sorry for being thick)
could you present your solution as code (like guestgulkan did) so
that i can follow your solution more easily.

Regards, EightBit.
Take a look at T here:
1
2
template <class T>
T maxn(const T * ar, int n)

What jsmith has said is that replacing T with char * does not result in this:
1
2
template <>
char * maxn<char*>(const char * ar, int n)      // line 53 

Last edited on
Hi,

Thank you moorecm for your explaination.
Unfortunately, i still don't get it; especially when guestgulkan
says it works for him.

This is one those problems when the solution will come to me
when i least expect it to.

Regards, Eightbit.
I'll double check to make sure the function I think is being called really is being called.
The point is that if you have this
1
2
template <typename T>
T maxn( const T * ar, int n);

And call maxn with T as char*: maxn<char*>
It replaces all the occurances of T, with char *,

leaving you with this:
1
2
template <>
char * maxn( const char* * ar, int n);


and like moorecm said not this, wich is what you want to do,
1
2
template <>
char * maxn<char*>(const char * ar, int n)
Last edited on
Topic archived. No new replies allowed.