Question

I'm trying to make a template function specialization for a bubble sort of character array's. For some reason though, when I'm about to define the function, I get an error underline over the function name and I have no idea why.

template<typename T>
T sort(T* a, T n) {
    int i, j;
    int temp;

    for (i = n - 1; i > 0; i--) {
        for (j = 0; j < i; j++) {
            if (a[j] > a[j + 1]) {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

template<>
const char* sort<const char*>(const char* a, const char* n) {

}
Was it helpful?

Solution

The problem:

When you replace T with const char *, the function signature looks like this:

const char* sort<const char*>(const char** a, const char* n)
                             //        ^^^ T* a -> const char ** a

Recommended:

Why is your signature template<typename T> T sort(T* a, T n) anyways? You aren't returning anything, and you are treating n as a size_t. I recommend changing your signature to this:

template<typename T> void sort(T* a, size_t n);

And your specialization to:

template<> void sort<char>(char* a, size_t n); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top