c++ - how to code an explicit specialization for a function template (bubble sorting array of strings ) -
to begin , pretty new c++ . have code function template explicit specialization sort array of integers, , array of strings .
my main program
#include <iostream> #include "sort.h" int main() { int a[] = { 1234, 546, 786, 2341 }; char* c[6] = { "harry", "jane", "anne", "john" }; sort(a, 4); sort(c, 4); (int = 0; < 4; i++) std::cout << a[i] << std::endl; std::cout << std::endl; (int = 0; < 4; i++) std::cout << c[i] << std::endl; std::cout << std::endl; }
sort.h file ( file containing template )
template<typename t, typename t1> void sort(t* items, t1 count) { t1 t; (int = 1; a<count; a++) (int b = count - 1; b >= a; b--) if (items[b - 1] > items[b]) { t = items[b - 1]; items[b - 1] = items[b]; items[b] = t; } }
problem here : says : "error c2912: explicit specialization 'void sort(char *,int)' not specialization of function template" getting confused " char * c[] " , how make specialization type .
template<> void sort<char ** >(char** p, int count) { }
you have match primary template signature exactly, e.g. so:
template <> void sort<char *, int>(char ** p, int count) { // ... }
so t = char *
, t1 = int
.
Comments
Post a Comment