Template probelem

Started by
6 comments, last by phresnel 15 years, 3 months ago
Hi. Been doing some programming problems from a book i bought some time ago but now i'm facing some problems with templates. the task is the following: write a template that you send and array of int and an array of double. Then make a specialisation of the template which takes an array of char pointers. so i wrote the following code:


#include <iostream>

using namespace std;

template <typename T>
T maxn(T vekt[], T size)
{
    T tempBig = vekt[0]; // currently biggest number

    for(int i = 0;i < size; i++)
    {

        if(vekt > tempBig)
        {

            tempBig = vekt;
        }
    }

    return tempBig;
}

template <> char* maxn(char vekt[], int size)
{
    int theLongest = strlen(vekt[0]); // the longest string
    int i = 0;

    while(i < ant)
    {
        i++;
        if(theLongest > strlen(vekt)
        {
            theLongest = strlen(vekt);
        }
    }

    return &theLongest; // returning the adress to the longest
}


int main()
{
    char *CharPtrVektor[5] = {"A", "AB", "ABC", "ABCD", "ABCDE"};
    int IntVektor[6] = {1, 2, 3, 4, 5, 6};
    double DoubleVektor[5] = {1.1, 2.2, 3.3, 4.4, 5.5};

    double arf = 5;
    int barf = 6;
    int sarf = 5;

    cout << maxn(DoubleVektor, arf) << endl;    // testing the functions
    cout << maxn(IntVektor, barf) << endl;
    cout << maxn(CharPtrVektor, sarf) << endl;

    return 0;
}


now, the errors i'm getting are the following: vningar\main.cpp|24|error: template-id `maxn<>' for `char* maxn(char*, int)' does not match any template declaration| vningar\main.cpp|24|error: invalid function declaration| vningar\main.cpp||In function `int main()':| vningar\main.cpp|53|error: no matching function for call to `maxn(char*[5], int&)'| ||=== Build finished: 3 errors, 0 warnings ===| it appears that i'm doing something wrong with the specialisations declaration but i can't see what. Help is greatly appreciated.
Advertisement
Your function doesn't take an array of char pointers. It takes an array of chars.
In order for the specialization to work, you need to exactly substitute the template parameters. So for T = char, it would be:
template <> char maxn(char vekt[], char size)


There probably isn't much point in trying to specialize a template function. Just provide a non-template overload for array of char arrays. Your implementation also seems to be horribly broken in many other places.

Also consider using std::string which provides comparison operators. Then you wouldn't need to specialize anything. (Assuming you realize it doesn't make sense to compare other types by the value and C-style strings by the length - logically one would expect to get back the largest string - hint: strcmp, not strlen -, not the longest. :))
"horribly broken" :(, but i guess your right, it was kind of sloppy. But anyway, i changed the code abit with your help but i still had problems getting the pointers to work in the specialisation, so i thought i might try doing it with references and it was quite a bit easier (even though that wasn't how i was supposed to solve the problem)

So, now my question is, when do you really need specialisations with templates?
To me it seems like you have a template when you really don't want to be specific, when you need a function to work with any variables. And why would you like to specify then?

thanks for the help anyway
Quote:
So, now my question is, when do you really need specialisations with templates?
To me it seems like you have a template when you really don't want to be specific, when you need a function to work with any variables. And why would you like to specify then?


Your exercise is one such example. Normally you can assume that if it is meaningful to get the max element for a type of items, the type provide comparison operators (suggestion: STL uses operator< by default in every order-depending functionality). Not C style strings where comparison operators also work but compare pointer values and therefore return meaningful results.

It seems to me that STL doesn't provide much help to users of C style strings. Although algorithms that depend on item comparison let you pass your own comparison function, strcmp as such cannot be used directly.

bool strcmp_less(const char* a, const char* b){   return std::strcmp(a, b) < 0;}...const char* strings[] = { ... }std::cout << *std::max_element(strings, strings + size, strcmp_less);
One reason it didn't compile is probably that you've got your size parameter in the general template being of type T, when really it should be int or size_t.
Vandevoorde/Josuttis gives solutions to this, but suggests to try to simply avoid char* and instead use std::string (whereas array in general are not a big problem).

This topic is closed to new replies.

Advertisement