c++How to write a driver program for a recursive function that returns sum of vector elements

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());
        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;

It's a const-correctness problem.

Given const vector<Object>& a, a.begin() is a vector<Object>::const_iterator, not a vector<Object>::iterator. Your sumVectorRecurse cannot accept a vector<Object>::const_iterator.

I would recommend that sumVectorRecurse take a vector<Object>::const_iterator because, though you cannot remove constness, you can generally add it!

Related Articles
You Might Also Like