I am working on a function that is comparing each term object in a vector to see if they are (alike) not being == that is for example 3x^2 + 4x^2 are alike terms. I am trying to find all like terms and add them so when I am doing going through the function each term that is left is different from the next and so on. Recursion would be a great way to do this since we do not know how many terms this polynomial will have. The trouble I am having is figuring out how to make the base case to break out of the recursion. This is the function I have so far. I use a function to sort them from greatest exponent to lest exponent. I will have to eventually change that function once I set up the highest order to the term if that term contains more than one variable for example: 3x^3*y^2 then this term would have an order of 5, but for now I am testing only using a single variable x in my terms.

`// ------------------------------------------------------------------------- //`

// combineLikeTerms()

//

template <class T>

void Polynomial<T>::combineLikeTerms() {

// First We Order Our Terms.

setPolynomialByDegreeOfExponent();

Term<T> t; // Temp Variable

std::vector<Term<T>> vTempTerms; // Temp storage

std::vector<Term<T>>::iterator it = _vTerms.begin();

std::vector<Term<T>>::iterator it2 = (_vTerms.begin()+1);

while ( it2 != _vTerms.end() ) {

for ( int i = 0; i < (int)_vTerms.size()-1; i++ ) {

if ( (*it).termsAreAlike( *it2 ) ) {

t = *it + *it2; // Add The Like Terms

vTempTerms.push_back( t ); // Push To Our Temp

it += 2; // Advance Two Locations

it2 += 2; // ""

} else {

t = *it; // Set Only First It To Our Temp

vTempTerms.push_back( t ); // Push To Our Temp

it++; // Advance Only One Location For Each

it2++;

}

}

}

// Set our Member Equal To Our Temp

_vTerms = vTempTerms;

// Reset Our Iterators

it = _vTerms.begin();

it2 = (_vTerms.begin()+1);

combineLikeTerms();

} // combineLikeTerms

Any help would be awesome!!!