I have a bit of a problem and I hope you guys are able to help. I have a function template that inserts a value into an array. Works fine if the value that's being inserted is a numerical value (int, double, float) problem is when I try to add a string to an array using this.
I'm aware that I basically can't pass a string to a function template, I just don't know where to go from there. Any help I can get would be greatly appreciated.
1 2 3 4 5 6 7
template <class T>void insert(T* data, T& n, t& x){
T i;
for ( i = n; i>0 && data[i-1] > string; i--)
data[i] = data[i-1];
data[i] = string;
++n;
}
In your function data is of type T* (string *) and is the array where you want to insert something, x is a T& (string&) and is the object you want to insert (note that c++ is case sensitive) and n is the position where you want to insert the element. However n is of type T& (string&). How do you expect to describe the position with a string? It should be template <class T>void insert(T* data, int n, T& x).