Function Template and string literals

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).
Sorry about the typos...didn't see them when I put my code in. It should have been
1
2
3
4
5
6
7
template <class T>void insert(T* data,int n, T& x){
T i;
	for ( i = n; i>0 && data[i-1] > string; i--)
		data[i] = data[i-1];
	data[i] = string;
	++n;
}


which still fails to work.
Last edited on
what is string? shouldn't be x ?

Edit: I guess this is how you are calling at your function
insert( array, n, "hello_world" );
error: no matching function for call to 'insert(std::string [42], int, const char [12])'

Construct a temporary string
insert( array, n, std::string("hello_world") );

Edit2: if you want to modify n inside the function, then pass it by reference. void insert(T* data, int &n, T& x)
Last edited on
Ah yes that is another typo. Sorry bout that.
1
2
3
4
5
6
7
template <class T>void insert(T* data,int n, T& x){
T i;
	for ( i = n; i>0 && data[i-1] > x; i--)
		data[i] = data[i-1];
	data[i] = x;
	++n;
}


I did try to both pass the string as a variable, and as a temporary string as you suggested. I get the same error either way.
what error is that?
also, show us with what arguments you are calling it.
The rest of the relevant code for this looks a little something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
template <class T>void insert(T* data, int& n,T& x );
int main(){
        string s2[100] = {"Paul", "Pat", "Jesse","Jessie", "Rob", "Bud", "Tony", "Tom", "Keo"};
	int z = 9;
	print(s2,z);
	string input = "Kurt";
	insert2(s2,z, input);
	print(s2,z);
return 0;


And the error is...could not deduce template argument for 'const _Elem *' from 'std::string'
well, unless line 2 of your insert ( T i; ) isn't a typo, I don't see anything wrong.
It isn't an error. It's just supposed to point to data[n-1]. I suppose it could just be an int, but I think it should work either way.

Edit: Actually I was wrong! I just tested the insert and changed that line to be an int instead of i and it works! Thanks Hamsterman for the pointer.
Last edited on
Topic archived. No new replies allowed.