3

I have class Queue that is implemented with templates, one parameter for the type and one constant parameter for the size of the queue.

  template <typename T, int N>
  class Queue
  {
     .....
     void enqueue(T x);
  }

I want to specialize the enqueue method, for the typename but I can not figure how to do this.

 template <typename T, int N>
 void Queue<Heap<struct infoNod>, N>::enqueue(Heap<struct infoNode> x)
 {}

For specializing the entire class I am not sure if i do it right: in header:

 template <>
 class Queue<Heap<struct infoNode>, 100>
 {
    public:
       void enqueue(Heap<struct infNode> x);
 }; 

in cpp:

template <>
 void Queue<Heap<struct infoNod>, 100>::enqueue(Heap<struct infoNode> x) {}

errors:

Queue.cpp:77:6: error: template-id ‘enqueue<>’ for ‘void Queue<Heap<infoNod>, 100>::enqueue(Heap<infoNode>)’ does not match any template declaration
 void Queue<Heap<struct infoNod>, 100>::enqueue(Heap<struct infoNode> x)
      ^
Queue.cpp:77:78: note: saw 1 ‘template<>’, need 2 for specializing a member function template
 void Queue<Heap<struct infoNod>, 100>::enqueue(Heap<struct infoNode> x)
Tas
  • 7,023
  • 3
  • 36
  • 51
georgiana_e
  • 1,809
  • 10
  • 35
  • 54
  • You can not specialize only a single member function, you need to specialize the whole class. Or create derivative class which inherits from `Queue, N>` and override only the `enqueue` function. – Some programmer dude Jan 16 '17 at 11:46
  • I tried to specialize the class, edited post. – georgiana_e Jan 16 '17 at 11:55
  • I think you meant `Queue`. Otherwise your enqueue function would accept a `Queue>, 100>` as parameter – Simon Kraemer Jan 16 '17 at 11:57
  • 1
    You need to show us the definition of the `enqueue` function that matches that error. And more importantly I think you need to read ["Why can templates only be implemented in the header file?"](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Some programmer dude Jan 16 '17 at 11:58
  • On a side-note, passing the argument `x` by value might not be a good idea. Instead pass by constant reference, i.e. `Heap const& x` instead. – Some programmer dude Jan 16 '17 at 12:00
  • definition of enqueue: void enqueue(T x) / void enqueue(Heap x); – georgiana_e Jan 16 '17 at 12:06

2 Answers2

1

Since you have a class template with a non-template enqueue() method, you can only partially specialize the whole class, not the individual method:

template <typename T, int N>
class Queue
{
    void enqueue(T x) { /* default implementation */ }
};

template<int N>
class Queue<Heap<InfoNode>, N>
{
   void enqueue(Heap<InfoNode> x) { /* specialized implementation */ }
};

Another alternative is not to specialize the whole Queue class but to make enqueue() delegate to a small helper class that will be specialized.

Joseph Artsimovich
  • 1,499
  • 10
  • 13
  • I specialized the Queue, and moved the implementation to the header. But if i specialize, can I access the methods and members from the unspecialized class. I have a method print in the unspecialized class, and i get error (error: ‘class Queue, 256>’ has no member named ‘print’) – georgiana_e Jan 16 '17 at 13:09
  • @georgiana_e: The general and the specialized classes are unrelated, so you won't be able to call one's methods from another. – Joseph Artsimovich Jan 16 '17 at 13:15
0

I have solved by extending the Queue class:

class QueueSpec: public Queue<Heap<struct infoNode>, SIZE >
{
    public:
        void enqueue(Heap<struct infoNode> x);
};

---
void QueueSpec::enqueue(Heap<struct infoNode> x) {}
georgiana_e
  • 1,809
  • 10
  • 35
  • 54