I cannot figure this out. Can anyone see what is causing the problem?

This is the error I get when I try to compile with the GNU compiler:

sortQS.h: In function âvoid sort(UList<U>&) [with U = int]â:
test.cpp:13: instantiated from here
sortQS.h:16: error: ârecQSâ was not declared in this scope

Header file UList.h containing template class:

#ifndef ULIST_H
#define ULIST_H

#include <vector>
#include <iostream>

template <class T>
class UList{

  public:

    template <class U>
    friend std::ostream& operator << (std::ostream&, const UList<U>&);

    template <class U>
    friend void sort (UList<U>&);
    //Defined in a separate header file, sortQS.h
    //Precondition:
    //  A list exists and is not empty
    //
    //Postcondition:
    //  The list is sorted in ascending order


    UList (size_t=10);
    virtual ~UList();
    void insert (const T&);
    bool erase (const T&);
    bool find (const T&) const;
    size_t size() const;
    bool empty() const;

  protected:

    std::vector<T> items;  //list of items

  private:

    void recQS (size_t, size_t);
    size_t partition (size_t, size_t);
    void swap (size_t, size_t);
};

//Definitions of non-private member functions omitted


template <class T>
void UList<T>::recQS (size_t first, size_t last){
  //Recursive function used to sort the list by quicksort
  //
  //Precondition:
  //  A list exists
  //
  //Postcondition:
  //  The list is sorted in ascending order

  size_t pivotLoc;

  if (first < last){
    pivotLoc = partition (first, last);
    recQS (first, pivotLoc - 1);
    recQS (pivotLoc + 1, last);
  }
}



template <class T>
size_t UList<T>::partition (size_t first, size_t last){
  //Used to partition and sort the list by quicksort
  //
  //Precondition:
  //  A list exists
  //
  //Postcondition:
  //  One item in the list is in its correct position for ascending order 

  T pivot;
  size_t index; //index of first element in unsorted partition of list
  size_t smIndex; //index of last element in lower partition of list

  //Swap first and middle elements in list
  swap (first, (first + last) / 2);

  pivot = items[first];
  smIndex = first;

  for (index = first + 1; index <= last; ++index){
    if (items[index] < pivot)
      swap (++smIndex, index);
  }

  swap (first, smIndex);

  return smIndex;
}



template <class T>
void UList<T>::swap (size_t first, size_t second){
  //Precondition:
  //  A list exists
  //
  //Postcondition:
  //  Two items in the list have traded positions

  T temp;

  temp = items[first];
  items[first] = items[second];
  items[second] = temp;
}
#endif

Header file sortQS.h containing friend function sort:

#ifndef SORTQS_H
#define SORTQS_H

#include "UList.h"

template <class U>
void sort (UList<U>& obj){
  recQS (0, obj.size()-1);
}
#endif

Thanks for any help.

I found the problem. In sortQS.h, the call should be obj.recQS(0, obj.size()-1). I just saw that after staring at it for an hour.

Line 8 of the sortQS.h you have posted recQS is a member of UList<U> but you do not call it on the object you call it as though it were a regular function try obj.recQS (0, obj.size()-1);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.