I run the program using heaps into which I have to embed Sort, mergeSort and quickSort into the heap. I was instructed to use the code from my tutorial as a basis, and I can’t even get the example code to compile. I keep getting the error:
not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
Now I think this has something to do with the order in which the template <> classes were declared in the Sort.h file. However, I cannot imagine a textbook publishing code that simply does not compile. Could you guys take a look?
Complete errors:
In file included from Driver.cpp:4:0:
Sort.h: In instantiation of 'void heapsort(std::vector<Comparable>&) [with Comparable = int]':
Driver.cpp:47:21: required from here
Sort.h:79:9: error: 'percDown' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
Sort.h:104:6: note: 'template<class Comparable> void percDown(std::vector<Comparable>&, int, int)' declared here, later in the translation unit
Sort.h:83:9: error: 'percDown' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
Sort.h:104:6: note: 'template<class Comparable> void percDown(std::vector<Comparable>&, int, int)' declared here, later in the translation unit
Sort.h: In instantiation of 'void mergeSort(std::vector<Comparable>&, std::vector<Comparable>&, int, int) [with Comparable = int]':
Sort.h:150:5: required from 'void mergeSort(std::vector<Comparable>&) [with Comparable = int]'
Driver.cpp:53:22: required from here
Sort.h:138:9: error: 'merge' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
Sort.h:163:6: note: 'template<class Comparable> void merge(std::vector<Comparable>&, std::vector<Comparable>&, int, int, int)' declared here, later in the translation unit
Sort.h:
#ifndef SORT_H
#define SORT_H
#include <vector>
#include <functional>
using namespace std;
template <typename Comparable>
void insertionSort( vector<Comparable> & a )
{
for( int p = 1; p < a.size( ); ++p )
{
Comparable tmp = std::move( a[ p ] );
int j;
for( j = p; j > 0 && tmp < a[ j - 1 ]; --j )
a[ j ] = std::move( a[ j - 1 ] );
a[ j ] = std::move( tmp );
}
}
template <typename Comparable>
void insertionSort( vector<Comparable> & a, int left, int right )
{
for( int p = left + 1; p <= right; ++p )
{
Comparable tmp = std::move( a[ p ] );
int j;
for( j = p; j > left && tmp < a[ j - 1 ]; --j )
a[ j ] = std::move( a[ j - 1 ] );
a[ j ] = std::move( tmp );
}
}
template <typename Comparable>
void shellsort( vector<Comparable> & a )
{
for( int gap = a.size( ) / 2; gap > 0; gap /= 2 )
for( int i = gap; i < a.size( ); ++i )
{
Comparable tmp = std::move( a[ i ] );
int j = i;
for( ; j >= gap && tmp < a[ j - gap ]; j -= gap )
a[ j ] = std::move( a[ j - gap ] );
a[ j ] = std::move( tmp );
}
}
template <typename Comparable>
void heapsort( vector<Comparable> & a )
{
for( int i = a.size( ) / 2 - 1; i >= 0; --i )
percDown( a, i, a.size( ) );
for( int j = a.size( ) - 1; j > 0; --j )
{
std::swap( a[ 0 ], a[ j ] );
percDown( a, 0, j );
}
}
inline int leftChild( int i )
{
return 2 * i + 1;
}
template <typename Comparable>
void percDown( vector<Comparable> & a, int i, int n )
{
int child;
Comparable tmp;
for( tmp = std::move( a[ i ] ); leftChild( i ) < n; i = child )
{
child = leftChild( i );
if( child != n - 1 && a[ child ] < a[ child + 1 ] )
++child;
if( tmp < a[ child ] )
a[ i ] = std::move( a[ child ] );
else
break;
}
a[ i ] = std::move( tmp );
}
template <typename Comparable>
void mergeSort( vector<Comparable> & a,
vector<Comparable> & tmpArray, int left, int right )
{
if( left < right )
{
int center = ( left + right ) / 2;
mergeSort( a, tmpArray, left, center );
mergeSort( a, tmpArray, center + 1, right );
merge( a, tmpArray, left, center + 1, right );
}
}
template <typename Comparable>
void mergeSort( vector<Comparable> & a )
{
vector<Comparable> tmpArray( a.size( ) );
mergeSort( a, tmpArray, 0, a.size( ) - 1 );
}
template <typename Comparable>
void merge( vector<Comparable> & a, vector<Comparable> & tmpArray,
int leftPos, int rightPos, int rightEnd )
{
int leftEnd = rightPos - 1;
int tmpPos = leftPos;
int numElements = rightEnd - leftPos + 1;
while( leftPos <= leftEnd && rightPos <= rightEnd )
if( a[ leftPos ] <= a[ rightPos ] )
tmpArray[ tmpPos++ ] = std::move( a[ leftPos++ ] );
else
tmpArray[ tmpPos++ ] = std::move( a[ rightPos++ ] );
while( leftPos <= leftEnd )
tmpArray[ tmpPos++ ] = std::move( a[ leftPos++ ] );
while( rightPos <= rightEnd )
tmpArray[ tmpPos++ ] = std::move( a[ rightPos++ ] );
for( int i = 0; i < numElements; ++i, --rightEnd )
a[ rightEnd ] = std::move( tmpArray[ rightEnd ] );
}
template <typename Comparable>
const Comparable & median3( vector<Comparable> & a, int left, int right )
{
int center = ( left + right ) / 2;
if( a[ center ] < a[ left ] )
std::swap( a[ left ], a[ center ] );
if( a[ right ] < a[ left ] )
std::swap( a[ left ], a[ right ] );
if( a[ right ] < a[ center ] )
std::swap( a[ center ], a[ right ] );
std::swap( a[ center ], a[ right - 1 ] );
return a[ right - 1 ];
}
template <typename Comparable>
void quicksort( vector<Comparable> & a, int left, int right )
{
if( left + 10 <= right )
{
const Comparable & pivot = median3( a, left, right );
int i = left, j = right - 1;
for( ; ; )
{
while( a[ ++i ] < pivot ) { }
while( pivot < a[ --j ] ) { }
if( i < j )
std::swap( a[ i ], a[ j ] );
else
break;
}
std::swap( a[ i ], a[ right - 1 ] );
quicksort( a, left, i - 1 );
quicksort( a, i + 1, right );
}
else
insertionSort( a, left, right );
}
template <typename Comparable>
void quicksort( vector<Comparable> & a )
{
quicksort( a, 0, a.size( ) - 1 );
}
template <typename Comparable>
void quickSelect( vector<Comparable> & a, int left, int right, int k )
{
if( left + 10 <= right )
{
const Comparable & pivot = median3( a, left, right );
int i = left, j = right - 1;
for( ; ; )
{
while( a[ ++i ] < pivot ) { }
while( pivot < a[ --j ] ) { }
if( i < j )
std::swap( a[ i ], a[ j ] );
else
break;
}
std::swap( a[ i ], a[ right - 1 ] );
if( k <= i )
quickSelect( a, left, i - 1, k );
else if( k > i + 1 )
quickSelect( a, i + 1, right, k );
}
else
insertionSort( a, left, right );
}
template <typename Comparable>
void quickSelect( vector<Comparable> & a, int k )
{
quickSelect( a, 0, a.size( ) - 1, k );
}
template <typename Comparable>
void SORT( vector<Comparable> & items )
{
if( items.size( ) > 1 )
{
vector<Comparable> smaller;
vector<Comparable> same;
vector<Comparable> larger;
auto chosenItem = items[ items.size( ) / 2 ];
for( auto & i : items )
{
if( i < chosenItem )
smaller.push_back( std::move( i ) );
else if( chosenItem < i )
larger.push_back( std::move( i ) );
else
same.push_back( std::move( i ) );
}
SORT( smaller );
SORT( larger );
std::move( begin( smaller ), end( smaller ), begin( items ) );
std::move( begin( same ), end( same ), begin( items ) + smaller.size( ) );
std::move( begin( larger ), end( larger ), end( items ) - larger.size( ) );
}
}
template <typename RandomIterator, typename Comparator>
void insertionSort( const RandomIterator & begin,
const RandomIterator & end,
Comparator lessThan )
{
if( begin == end )
return;
RandomIterator j;
for( RandomIterator p = begin+1; p != end; ++p )
{
auto tmp = std::move( *p );
for( j = p; j != begin && lessThan( tmp, *( j-1 ) ); --j )
*j = std::move( *(j-1) );
*j = std::move( tmp );
}
}
template <typename RandomIterator>
void insertionSort( const RandomIterator & begin,
const RandomIterator & end )
{
insertionSort( begin, end, less<decltype(*begin )>{ } );
}
#endif
And Driver.cpp
//Driver.cpp
#include <iostream>
#include "Sort.h"
#include <vector>
#include <string>
#include <time.h>
using namespace std;
void checkSort( const vector<int> & a )
{
for( int i = 0; i < a.size( ); ++i )
if(a.size( ) != i )
cout << "Error at " << i << endl;
cout << "Finished checksort" << endl;
}
int main( )
{
srand(time(NULL));
int vec_length;
cout << "What would you like the length of the vector to be?" << endl;
cin >> vec_length;
vector<int> a(vec_length);
for(int i = 0; i < a.size(); i++)
{
a[i] = rand() % 100 + 1;
}
for( int theSeed = 0; theSeed < 10; ++theSeed )
{
insertionSort( a );
checkSort( a );
insertionSort( begin( a ), end( a ) );
checkSort( a );
heapsort( a );
checkSort( a );
shellsort( a );
checkSort( a );
mergeSort( a );
checkSort( a );
quicksort( a );
checkSort( a );
SORT( a );
checkSort( a );
quickSelect( a, vec_length / 2 );
}
cout << "Checking SORT, Fig 7.13" << endl;
int N = vec_length * vec_length;
vector<int> b( N );
for( int i = 0; i < N; ++i )
b[ i ] = i;
SORT( b );
for( int i = 0; i < N; ++i )
if( b[ i ] != i )
cout << "OOPS!!" << endl;
return 0;
}