I'm attempting to use a specialized function to search an array of c strings for the longest (or first tied for longest) member. Right now, I'm getting line 87 error: invalid conversion from `char' to `const char*'| on my comparison statement. If I switch the templates' inputs to const, I seem to be unable to get my templates' return functions to work. Where am I going wrong?

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

const int numInts = 6;
const int numDubs = 4;
const int numStrs = 5;
const int maxLength = 80;

template <typename T>
T * maxn(T * a, const int n);
template <> char * maxn<char>(char * a, const int n);


int main()
{
    cout << "Enter " << numInts << " integers, please.\n";

    int ints[numInts];
    for (int i = 0; i < numInts; i++)
    {
        cout << "Int " << i+1 << ":";
        while(!(cin >> ints[i]))
        {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Integers only, please.\n";
            cout << "Int " << i+1 << ":";
        }
    }
    cout << "The largest of those is " << *maxn(ints, numInts) << ".\n\n";
    cin.clear();
    cin.ignore(1000, '\n');

    cout << "Enter " << numDubs << " doubles, please.\n";
    double dubs[numDubs];
    for (int i = 0; i < numDubs; i++)
    {
        cout << "Double " << i+1 << ":";
        while(!(cin >> dubs[i]))
        {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Doubles only, please.\n";
            cout << "Double " << i+1 << ":";
        }
    }
    cout << "The largest of those is " << *maxn(dubs, numDubs) << ".\n\n";
    cin.clear();
    cin.ignore(1000, '\n');

    cout << "Enter " << numStrs << " strings, please.\n";
    char strs[numStrs][maxLength];
    for (int i = 0; i < numStrs; i++)
    {
        cout << "String " << i+1 << ":";
        while(!(cin.getline(strs[i], maxLength)))
        {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Something didn't fit.  Try again.\n";
            cout << "String " << i+1 << ":";
        }
    }
    cout << "The largest of those is " << *maxn(strs, numStrs) << ".\n\n";

    return 0;
}

template <typename T>
T * maxn(T * a, const int n)
{
    int longest = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[i] > a[longest])
                longest = i;
    }
    return &a[longest];
}

template <> char * maxn<char>(char * a, const int n)
{
    int longest = 0;
    for (int i = 1; i < n; i++)
    {
        if(std::strlen(a[i]) > std::strlen(a[longest]))
            longest = i;
    }
    return &a[longest];
}

Recommended Answers

All 4 Replies

In order to get done what you want done, you'd have to make an array of pointers...

//This will result in the 'const char' that you are trying to avoid
char strs[numStrs][maxLength];

//Try this:
//Dynamically allocated 2d array
char** strs = new char*[numStrs];
for(int i=0; i<numStrs; i++)
     strs[i] = new char[maxLength];

//Now you should be able to pass in the array as a 'cstring'(char*) 
//as opposed to a 'char literal' (const char array)
if(std::strlen(a[i]) > std::strlen(a[longest]))

I've added the dynamic array in; a quick cout for-loop test shows it is storing cstrings properly. The template function is still giving me "invalid conversion from 'char' to 'const char*' , on the strlen line, though. Do I need to change the way I'm sending those values to the functions or something?

Here another idea..

The problem might be in line #71, where you pass in an argument as a char*

I'm thinkin' since you are using a 2d array, maybe try passing stuff in as a char**

T** maxn(T** a, const int n)

You could try changing the declaration of the template to:

template <typename T>
T maxn(T a[], const int n);

This makes it more obvious what the specialisation should be:

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

You can then create an array of strings to pass in

cout << "Enter " << numStrs << " strings, please.\n";
    char* strs[numStrs];
    for(int i=0; i<numStrs; i++)
        strs[i] = new char[maxLength];
    for (int i = 0; i < numStrs; i++)
    {
        cout << "String " << i+1 << ":";
        while(!(cin.getline(strs[i], maxLength)))
        {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Something didn't fit.  Try again.\n";
            cout << "String " << i+1 << ":";
        }
    }
    cout << "The largest of those is " << maxn(strs, numStrs) << ".\n\n";

Note that maxn returns a T rather than a T*, so you don't have to dereference it.
Hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.