Template specialization error - C ++ (C ++ Primer Plus exercise)
I am currently learning C ++ so I have little knowledge on this topic. I am using C ++ - primer plus book and here is the problem:
Write a template function maxn () that takes an array of elements of type T as its arguments, and an integer representing the number of elements in the array and returning the largest element in the array. Check it out in a program that uses a function template with an array of six int values ββ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 a number of pointers as its second argument and returns the address of the longest string. If multiple lines are bound to a length, the function should return the address of the first one that was bound longer. Test your 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 am trying to pass an array of strings. I am getting 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 sure I made a newbie mistake and I can't spot it. Thank.
char (*)[10]
- pointer to an array of 10 characters. char *[10]
is an array of 10 char pointers.
Also you specify a different return type than TIe if the function should return char*
, the value for T must also be char*
. Your specialization should look like this:
template <> char * maxn<char *> (char *arr[] , int n);
Also your array of strings must be of type char *[5]
.
source to share
The program must also include a specialization that takes an array of pointers-to-char as an argument and a number of pointers as a second argument and returns the address of the longest string.
What you have wrong in your code is a two dimensional character array (one block of 5 * 6 bytes of memory). Compare with an array of five pointers.
const char* strings[5] = {"asta" , " m" , "ta" , "taree" , "e"};
Your code also makes the assumption that 0 is the smallest value for any T.
source to share
#include <iostream>
#include <cstring>
#include <locale>
//#include <windows.h>
using namespace std;
const int maxcharval = 5;
const int maxintval = 6;
const int maxdoubleval = 4;
template <typename T>
T maxn(T* arr, int n);
template <> const char * maxn <const char *> (const char* arr[], int n);
int main(int argc, char *argv[])
{
//setlocale(LC_CTYPE, ".866");
const char * charr[] = {"qwer","qwert","qwe","qw","q"};
const int intarr[] = {1,3,2,5,3,0};
const double doublearr[] = {5.4, 2.3, 3.1, 3.2};
cout << "maxint: " << maxn(intarr, maxintval) << endl;
cout << "maxdouble: " << maxn(doublearr, maxdoubleval)
<< endl;
cout << "maxcharstring:" << maxn(charr, maxcharval)
<< endl;
//system("pause");
return 0;
}
template <typename T>
T maxn(T *arr, int n)
{
T* value = &arr[0];
for (int i = 1; i < n; i++)
{
if (*value < arr[i])
value = &arr[i];
}
return *value;
}
template <> const char * maxn <const char *>(const char* arr[], int n)
{
const char* val = arr[0];
for (int i = 1; i < n; i++)
{
if (strlen(val) < strlen(arr[i]))
val = arr[i];
}
return val;
}
It works. Good luck!
source to share