0

My task is to create a priority queue template class and show its work using different data types: int, string and any struct. I have to add, delete, print a specific element.

The class is:

template< class Type >
class PriorityList
{
    private:
    List<Type> elems;

As a struct I picked was this:

struct SOMESTRUCT
{
    int num;
    char word[];
};

As I understand a template class is a universal class which can be used for any incoming data type. I can see how to create the class for int and char [], but how do I make it work for struct too? Since you can't just write cout<< struct, neither cin>>... I'd have to create another function for inputting/outputting my struct, but if I put it in my template class, the class wouldn't be universal again.

What do I do? Also, do I have to make template or leave just one typename?

Krish Munot
  • 1,093
  • 2
  • 18
  • 29
Mary
  • 45
  • 8
  • Possible duplicate of [How to properly overload the << operator for an ostream?](http://stackoverflow.com/questions/476272/how-to-properly-overload-the-operator-for-an-ostream) – wally Dec 04 '16 at 13:03
  • [This](https://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx) might also help. – wally Dec 04 '16 at 13:04
  • Hmm.. thank you for the option! But then I should also create my own Int and Char classes, right? So I can then write template , right? – Mary Dec 04 '16 at 13:11
  • 1
    @Mary ... what? – melpomene Dec 04 '16 at 13:15
  • Please post the full program. – wally Dec 04 '16 at 13:26
  • @melpomene To make the template class universal, I have to make my own Int and Char too, right? – Mary Dec 04 '16 at 13:27
  • @flatmouse Unfortunately, there's nothing to post yet. – Mary Dec 04 '16 at 13:28
  • @Mary No, why do you think so? – melpomene Dec 04 '16 at 13:28
  • Write a first program only with `int`. Then convert it to a template, then deal with the `struct`. If you're asking for a full template to be written for you in an answer then the question is too broad at this stage. – wally Dec 04 '16 at 13:31
  • is just a keyword. It accepts any type. PriorityList should just work. – Dragos Pop Dec 04 '16 at 13:37
  • @flatmouse Nope, I'm not. I was just struggling with template<...> thing. Okay, I'll try that, thank you – Mary Dec 04 '16 at 13:37

1 Answers1

1

You can use

template<typename C>
class PriorityList
{
// ...
};

for any type C, including int, char, SOMESTRUCT. I would recommend against using cin and cout in your template, since it makes it harder to use for any class. You would need to overload operator<< and operator>> for each class you use, which might be tricky.

Instead you should just define some simple member functions. I would try to copy the style of std::queue<T>, since people will be familiar with it.

For example:

template<typename C>
class PriorityQueue
{
    public: 
    void push(const C& item, int priority=0){
    // ...
    }
    C& front(){
    // ...
    }
    // and other required functions
};

Once it is defined, you can test it for different classes however you want. You can e.g. create a main() to declare a PriorityQueue<int>, read some ints from std::cin and then call those functions you defined, or you can just declare some instances of SOMESTRUCT and pass them into a PriorityQueue<SOMESTRUCT>.

In a real-life project you would probably use a proper unit testing framework such as catch for this purpose, which would automatically check everything works.

Joseph Ireland
  • 2,465
  • 13
  • 21