This is a recursive function that works fine. It sums up all things in a vector. The specs for the driver are: Create a driver function to call the recursive function; the driver function returns the value returned by the recursive function. The driver function takes one parameter of type vector and returns an Object. The DRIVER is giving an error that says "No matching function to call for sumVectorRecurse" on it's LAST line. Any ideas how to solve this or improve the driver itself?
//recursive function
template<class Object>
Object sumVectorRecurse(typename vector<Object>::iterator left, typename vector<Object>::iterator right)
{
if (left != right){
int midPosition = (right - left)/2;
typename vector<Object>::iterator mid = left + midPosition;
return (sumVectorRecurse<Object>(left, mid) + sumVectorRecurse<Object>(mid+1, right));
}
else return *left;
}
//driver routine
template<class Object>
Object sumVector(const vector<Object> & a){
if (a.size()==1){
return *(a.begin());
}
else
return sumVectorRecurse<Object>(a.begin(), a.end()-1);
}
int main(){
vector<int> v = {1,2,3,-4,5};
cout << endl << "THIS IS QUESTION 4:"<< endl;
sumVector(v);
}
Aucun commentaire:
Enregistrer un commentaire