views:

565

answers:

2

Hi!

I'm currently learning C++ so I don't have much knowledge on the topic . I'm using the C++ primer plus book and here's the problem :

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.

Here's my code :

#include <iostream>
#include <cstring>
using namespace std;

template <class T> T maxn(T arr[] , int n);
template <> char * maxn<char (*)[10]> (char (*arr)[10] , int n);

int main()
{
    double array[5] = { 1.2 , 4.12 ,7.32 ,2.1 ,3.5};
    cout << endl << maxn(array , 5) << endl << endl;

    char strings[5][6] = { "asta" , " m" , "ta" , "taree" , "e"};
    cout << maxn(strings , 5) << endl;

    return 0;
}

template <class T> T maxn(T arr[] , int n)
{
    T max = 0;
    for (int i = 0 ; i < n ; ++i)
    {
     if (arr[i] > max)
     max = arr[i];
    }
    return max;

}

template <> char * maxn<char (*)[10]> (char (*arr)[10] , int n)
{
    int length = 0;
    int mem = 0;
    for ( int i = 0 ; i < n ; ++i)
    {
     if (strlen(arr[i]) > length)
     {
      length = strlen(arr[i]);
      mem = i;
     }
    }
    return arr[mem];
}

I'm trying to pass an array of strings . I get the following errors :

    g++ -Wall -o "untitled5" "untitled5.cpp" (in directory: /home/eukristian)
untitled5.cpp:6: error: template-id ‘maxn<char (*)[10]>’ for ‘char* maxn(char (*)[10], int)’ does not match any template declaration
untitled5.cpp: In function ‘int main()’:
untitled5.cpp:14: error: no matching function for call to ‘maxn(char [5][6], int)’
untitled5.cpp: At global scope:
untitled5.cpp:31: error: template-id ‘maxn<char (*)[10]>’ for ‘char* maxn(char (*)[10], int)’ does not match any template declaration
Compilation failed.

I'm quite sure I've made some newbie mistake and I can't detect it . Thanks .

+6  A: 

char (*)[10] is a pointer to an array of 10 chars. char *[10] is an array of 10 char pointers.

Also you're specifying a different type for the return value than for T. I.e. if the function is supposed to return char*, the value for T should be char*, too. Your specialization should look like this:

template <> char * maxn<char *> (char *arr[] , int n);

Also your array of strings should be of type char *[5].

sepp2k
+1  A: 

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.

What you have in your code is not that, it's a two-dimensional array of characters (a single block of 5 * 6 bytes of memory). Compare with array of five pointers

const char* strings[5] = {"asta" , " m" , "ta" , "taree" , "e"};

Your code is also making the assumption that 0 is the smallest value for any T.

UncleBens