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

#include<iostream>
#include<string>

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;
			rIndex=i;
	}
	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;
cin.get();

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

}

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.