Help with template specialization

This is a discussion on Help with template specialization within the C++ Programming forums, part of the General Programming Boards category; Can someone else help spot what is wrong with the following code: Code: //prototypes template <class Any> Any maxn(Any ar[], ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    14

    Exclamation Help with template specialization

    Can someone else help spot what is wrong with the following code:

    Code:
    //prototypes
    template <class Any>
    Any maxn(Any ar[], int n);
    
    template <> Any maxn(char* ar[], int n);
    Here are the errors I get with Dev C++:
    11: expected constructor, destructor, or type conversion before "maxn"
    11: expected `;' before "maxn"
    43: expected constructor, destructor, or type conversion before "maxn"
    43: expected `;' before "maxn"

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    I am trying to create a specialization for the pointer-to-char type.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You specify the Any template parameter as the return type & first parameter of the function, but when you specialized it you only specialized the first parameter as a char*[] but not the return type.

    I am trying to create a specialization for the pointer-to-char type.
    Then you need to change your code, because right now it's specialized on an array of strings, not a single string.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Quote Originally Posted by cpjust View Post
    You specify the Any template parameter as the return type & first parameter of the function, but when you specialized it you only specialized the first parameter as a char*[] but not the return type.


    Then you need to change your code, because right now it's specialized on an array of strings, not a single string.
    Here is the entire code:

    Code:
    //prototypes
    template <class Any>
    Any maxn(Any ar[], int n);
    
    template <> Any maxn(char ar[], int n);
    
    int main() {
        using namespace std;
        int box1[6] = {5, 7, 3, 1, 8, 14};
        double box2[4] = {34.54, 12.656, 78.44, 66.08};
        char * box3[5] = {"Hello World!", "It's a great day.", "The weather is sunny.",
                            "How are you?", "Have a good day."};
        
        cout << "Use maxn() function for integers. Largest element in the array ";
        cout << "of integers is: " << maxn(box1, 6) << endl;
        
        cout << "Use maxn() function for double values. Largest element in the array ";
        cout << "of double values is: " << maxn(box2, 4) << endl;
        
        cout << "Use maxn() function for array of char *. Return address of the longest ";
        cout << "string: " << maxn(box3, 5) << endl;
        
        system("pause");
        return 0;
    }
    
    template <class Any>
    void maxn(Any ar[], int n) {
        using namespace std;
        Any temp = ar[0];
        for(int i = 1; i < n; i++) {
            if(temp < ar[i]) {
                temp = ar[i]; } }
        return temp;
    }
    
    template <> Any maxn(char ar[], int n) {
        using namespace std;
        char * temp = ar[0];
        for(int i = 1; i < n; i++) {
            if(strlen(temp) < strlen(ar[i])) {
                temp = ar[i]; } }
        return *temp;
    }
    The specialization for the array of pointer-to-char needs to return the address of the longest string.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want an array of strings, then char *ar[] is fine. But you need to return something specific, presumably a char *.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Ok, I changed the return type to char*, but now I am getting the following errors:
    [Linker error] undefined reference to `int maxn<int>(int*, int)'
    [Linker error] undefined reference to `double maxn<double>(double*, int)'

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your template function at the bottom returns void instead of Any?

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Your template function at the bottom returns void instead of Any?
    I fixed it, and the code now compiles. Thanks.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    14
    Ok, I did not fix everything. How can I get the following function to return an address for the longest string int the array of char*:

    Code:
    template <> char* maxn(char* ar[], int n) {
        using namespace std;
        char * temp = ar[0];
        for(int i = 1; i < n; i++) {
            if(strlen(temp) < strlen(ar[i])) {
                temp = ar[i]; } }
        return temp;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In order to get your code to compile, I had to ditch the prototypes and move the function definitions up to the top (otherwise I was getting an error about specialization after instantiation). Doing so, I got the correct answer. I am not wise enough in the ways of template prototypes to say whether you can make what you have work.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    STL itself in these cases provides two overloads: the second one takes a comparison function. Everything that can be compared with operator < works fine (including std::string). If you want more unusual comparisons or work with C-style strings you do more work and provide a comparison function.

    In this particular case this approach might be more justified: you seem to want an unusual comparison for C style strings. Comparing them by length and not by the value of contents as you compare every other type seems so specific that it isn't justified as a specialization.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 09:11 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 02:02 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21