c++ - "Not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation" -


i'm beginning program using heaps in must insertsort, mergesort, , quicksort heap. instructed use code textbook basis , cannot example code compile. keep receiving error:

not declared in scope, , no declarations found argument-dependent lookup @ point of instantiation 

now think has order in template<> classes declared in sort.h file. however, can't imagine textbook publish code doesn't compile. guys mind taking look?

full errors:

in file included driver.cpp:4:0: sort.h: in instantiation of 'void heapsort(std::vector<comparable>&) [with comparable = int]': driver.cpp:47:21:   required here sort.h:79:9: error: 'percdown' not declared in scope, , no declarations found argument-dependent lookup @ point of instantiation [-fpermissive] sort.h:104:6: note: 'template<class comparable> void percdown(std::vector<comparable>&, int, int)' declared here, later in translation unit sort.h:83:9: error: 'percdown' not declared in scope, , no declarations found argument-dependent lookup @ point of instantiation [-fpermissive] sort.h:104:6: note: 'template<class comparable> void percdown(std::vector<comparable>&, int, int)' declared here, later in 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 'void mergesort(std::vector<comparable>&) [with comparable = int]' driver.cpp:53:22:   required here sort.h:138:9: error: 'merge' not declared in scope, , no declarations found argument-dependent lookup @ 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 translation unit 

sort.h:

#ifndef sort_h #define sort_h  /**  * several sorting routines.  * arrays rearranged smallest item first.  */  #include <vector> #include <functional> using namespace std;  /**  * simple insertion sort.  */ template <typename comparable> void insertionsort( vector<comparable> & ) {     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 );     } }   /**  * internal insertion sort routine subarrays  * used quicksort.  * array of comparable items.  * left left-most index of subarray.  * right right-most index of subarray.  */ 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 );     } }    /**  * shellsort, using shell's (poor) increments.  */ template <typename comparable> void shellsort( vector<comparable> & ) {     for( int gap = a.size( ) / 2; gap > 0; gap /= 2 )         for( int = gap; < a.size( ); ++i )         {             comparable tmp = std::move( a[ ] );             int j = i;              for( ; j >= gap && tmp < a[ j - gap ]; j -= gap )                 a[ j ] = std::move( a[ j - gap ] );             a[ j ] = std::move( tmp );         } }  /**  * standard heapsort.  */ template <typename comparable> void heapsort( vector<comparable> & ) {     for( int = a.size( ) / 2 - 1; >= 0; --i )  /* buildheap */         percdown( a, i, a.size( ) );     for( int j = a.size( ) - 1; j > 0; --j )     {         std::swap( a[ 0 ], a[ j ] );               /* deletemax */         percdown( a, 0, j );     } }  /**  * internal method heapsort.  * index of item in heap.  * returns index of left child.  */ inline int leftchild( int ) {     return 2 * + 1; }  /**  * internal method heapsort used in  * deletemax , buildheap.  * position percolate down.  * n logical size of binary heap.  */ template <typename comparable> void percdown( vector<comparable> & a, int i, int n ) {     int child;     comparable tmp;      for( tmp = std::move( a[ ] ); leftchild( ) < n; = child )     {         child = leftchild( );         if( child != n - 1 && a[ child ] < a[ child + 1 ] )             ++child;         if( tmp < a[ child ] )             a[ ] = std::move( a[ child ] );         else             break;     }     a[ ] = std::move( tmp ); }  /**  * internal method makes recursive calls.  * array of comparable items.  * tmparray array place merged result.  * left left-most index of subarray.  * right right-most index of subarray.  */ 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 );     } }  /**  * mergesort algorithm (driver).  */ template <typename comparable> void mergesort( vector<comparable> & ) {     vector<comparable> tmparray( a.size( ) );      mergesort( a, tmparray, 0, a.size( ) - 1 ); }   /**  * internal method merges 2 sorted halves of subarray.  * array of comparable items.  * tmparray array place merged result.  * leftpos left-most index of subarray.  * rightpos index of start of second half.  * rightend right-most index of subarray.  */ 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;      // main loop     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 )    // copy rest of first half         tmparray[ tmppos++ ] = std::move( a[ leftpos++ ] );      while( rightpos <= rightend )  // copy rest of right half         tmparray[ tmppos++ ] = std::move( a[ rightpos++ ] );      // copy tmparray     for( int = 0; < numelements; ++i, --rightend )         a[ rightend ] = std::move( tmparray[ rightend ] ); }   /**  * return median of left, center, , right.  * order these , hide pivot.  */ 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 ] );          // place pivot @ position right - 1     std::swap( a[ center ], a[ right - 1 ] );     return a[ right - 1 ]; }  /**  * internal quicksort method makes recursive calls.  * uses median-of-three partitioning , cutoff of 10.  * array of comparable items.  * left left-most index of subarray.  * right right-most index of subarray.  */ template <typename comparable> void quicksort( vector<comparable> & a, int left, int right ) {     if( left + 10 <= right )     {         const comparable & pivot = median3( a, left, right );              // begin partitioning         int = left, j = right - 1;         for( ; ; )         {             while( a[ ++i ] < pivot ) { }             while( pivot < a[ --j ] ) { }             if( < j )                 std::swap( a[ ], a[ j ] );             else                 break;         }          std::swap( a[ ], a[ right - 1 ] );  // restore pivot          quicksort( a, left, - 1 );     // sort small elements         quicksort( a, + 1, right );    // sort large elements     }     else  // insertion sort on subarray         insertionsort( a, left, right ); }  /**  * quicksort algorithm (driver).  */ template <typename comparable> void quicksort( vector<comparable> & ) {     quicksort( a, 0, a.size( ) - 1 ); }   /**  * internal selection method makes recursive calls.  * uses median-of-three partitioning , cutoff of 10.  * places kth smallest item in a[k-1].  * array of comparable items.  * left left-most index of subarray.  * right right-most index of subarray.  * k desired rank (1 minimum) in entire array.  */ 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 );              // begin partitioning         int = left, j = right - 1;         for( ; ; )         {             while( a[ ++i ] < pivot ) { }             while( pivot < a[ --j ] ) { }             if( < j )                 std::swap( a[ ], a[ j ] );             else                 break;         }          std::swap( a[ ], a[ right - 1 ] );  // restore pivot              // recurse; part changes         if( k <= )             quickselect( a, left, - 1, k );         else if( k > + 1 )             quickselect( a, + 1, right, k );     }     else  // insertion sort on subarray         insertionsort( a, left, right ); }  /**  * quick selection algorithm.  * places kth smallest item in a[k-1].  * array of comparable items.  * k desired rank (1 minimum) in entire array.  */ 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 & : items )         {             if( < chosenitem )                 smaller.push_back( std::move( ) );             else if( chosenitem < )                 larger.push_back( std::move( ) );             else                 same.push_back( std::move( ) );         }          sort( smaller );     // recursive call!         sort( larger );      // recursive call!          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( ) );  /*         items.clear( );         items.insert( end( items ), begin( smaller ), end( smaller ) );         items.insert( end( items ), begin( same ), end( same ) );         items.insert( end( items ), begin( larger ), end( larger ) ); */     } }  /*  * more public version of insertion sort.  * requires pair of iterators , comparison  * function object.  */ 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 );     } }  /*  * two-parameter version calls 3 parameter version, using c++11 decltype  */ 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> & ) {     for( int = 0; < a.size( ); ++i )         if(a.size( ) != )             cout << "error @ " << << endl;     cout << "finished checksort" << endl; }  int main( ) {     srand(time(null));       int vec_length;      cout << "what length of vector be?" << endl;     cin >> vec_length;      vector<int> a(vec_length);      for(int = 0; < a.size(); i++)     {         a[i] = rand() % 100 + 1;     }      //vector<string> a( num_items );        // input adds factor of n running time     //for( int = 1; < a.size( ); ++i )  // want test std::move logic       //  a[ ] = a[ - 1 ] + 'a';      for( int theseed = 0; theseed < 10; ++theseed )     {         insertionsort( );         checksort( );          insertionsort( begin( ), end( ) );         checksort( );          heapsort( );         checksort( );          shellsort( );         checksort( );          mergesort( );         checksort( );          quicksort( );         checksort( );          sort( );         checksort( );          quickselect( a, vec_length / 2 );         //cout << a[vec_length / 2 - 1].size( ) << " " << vec_length / 2 << endl;     }      cout << "checking sort, fig 7.13" << endl;     int n = vec_length * vec_length;     vector<int> b( n );     for( int = 0; < n; ++i )         b[ ] = i;     sort( b );     for( int = 0; < n; ++i )         if( b[ ] != )             cout << "oops!!" << endl;      return 0; } 

the order of declarations in c++ important. template functions in particular have declared before they're used. solution move definition e.g. percdown higher in file first use.


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -