So im in a collage c++ class, and i missed my last class, so my teacher told me to try lab 5, i think i have templates down but im getting a compiler error saying that my function is ambigious..
the exact error is

Error 1 error C2782: 'T searchArray(T,T,int)' : template parameter 'T' is ambiguous
c:\documents and settings\psdao01\my documents\visual studio 2008\projects\lab5\lab5\driver.cpp 44 Lab5


using namespace std;

template <class T>
T searchArray(T index, T answer, int numOfParts){
	int rIndex=-1;
	for(int i=0,i<numOfParts,i++){
		if index[i]=answer;
	return rIndex;

int main(){
const int numOfParts = 7;
string items[numOfParts];	
//create numOfParts items

/***** the below initializes the structure with part numbers
and names ****/
int items2[numOfParts];
items[0] = "AA"; 
items2[0]= 6;
items[1] = "BB"; 
items2[1]= 5;
items[2] = "CC"; 
items2[2]= 4;
items[3] = "DD"; 
items2[3]= 3;
items[4] = "EE"; 
items2[4]= 2;
items[5] = "FF"; 
items2[5]= 1;
items[6] = "GG"; 
items2[6]= 0;
string strEnd;

string itemA = "GG";				 
int itemB = 5;

cout <<"The first item was found at:"; 
cout << searchArray(items,itemA,numOfParts)<<endl;

cout <<"The second item was found at:"; 
cout << searchArray(items2,itemB,numOfParts)<<endl;
cout <<"Thank you" <<endl;

return 0;				//return the value 0 to the OS

8 Years
Discussion Span
Last Post by thelamb

Change your template to this :

template <class T, class T2>
T searchArray(T index, T2 answer, int numOfParts){

And let's explain why:

On line 44, you call searchArray with items and itemA.
items and itemA both have different types, but your searchArray expects them to be the same type (both of type 'T'). If you apply firstPerson's fix, 'answer' and 'index' can be of different, or the same type.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.