Jump to content
  • Advertisement
Sign in to follow this  
Skarrgan

Template probelem

This topic is 3455 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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)


Share this post


Link to post
Share on other sites
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. :))

Share this post


Link to post
Share on other sites
"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

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!