Thread: class templates

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    class templates

    Hi,
    I have an exercise to create a priority queue solely using an std::vector container from STL. The PQ itself works fine, but there are two associated problems left which I don't know how to solve...
    We've been given a file containing the main() which does the following thing:
    Code:
    #include "priorityqueue.h"
    
    template<class Type>
    class LessThan {
    
    public:
    bool operator()(const Type& el1, const Type& el2) { return el1 < el2; }
    }; int main() {
    PriorityQueue< std::string > h; h.push(..); // a few times std::cout << h.pop() << std::endl; // for all the elements in the queue PriorityQueue<std::string, LessThan<std::string> > h; // like above
    }
    And I created the necessary class in "priorityqueue.h" like this:
    Code:
    template<typename T, class Compare>
    class PriorityQueue {
    
    // ... class BinaryHeap {
    friend class PriorityQueue // ...
    };
    };
    The second PQ from the main will work fine like this, but with the first it (logically) gives the error "error: wrong number of template arguments (1, should be 2)". Is there a way to put the "class Compare" to the default compare operator when the second argument isn't given when the PQ is defined?
    Now, the PQ works like it should when I give the LessThan class along when defining the queue. The thing I wonder is whether I still need to use the class in some way to overload the compare operator, or will this automatically be done by the compiler? Meaning that when a function of the nested BinaryHeap class compares two elements in the heap, it will use the overloaded operator.

    I hope I'm a bit clear and understandable. I'm never any good at explaining problems in programming, especially in English .

    Thanks,
    Edward.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Templates can have default arguments:
    Code:
    template<typename T, class Compare = LessThan<T> >
    class PriorityQueue {
      // ...
      class BinaryHeap {
        friend class PriorityQueue
        // ...
      };
    };
    I'm sure you can figure out how to get it working from here.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Thanks a lot. That's all I needed to know .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Class templates and iterators
    By creativeinspira in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2007, 03:35 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM