1

I am new to the c++ language and need help with the problem below. Presently, I am trying to get my head around "templates" and "function pointers". The following template class, Queue, compiles when written into a single .cpp file:

template <typename T> class Queue
{
  public:
    Queue()
    {
        m_add = m_remove = 0;
    }
    void enque(T *c)
    {
        m_array[m_add] = c;
        m_add = (m_add + 1) % SIZE;
    }
    T *deque()
    {
        int temp = m_remove;
        m_remove = (m_remove + 1) % SIZE;
        return m_array[temp];
    }
  private:
    enum
    {
        SIZE = 8
    };
    T *m_array[SIZE];
    int m_add, m_remove;
};

However, when I separate this code into the .h and .cpp files (as below), I get errors where I am defining the function pointer in the .cpp file; near the line:

template<typename T>
T (Queue<T>::*deque)() {

Queue.h:

#ifndef QUEUE_H_
#define QUEUE_H_

template<typename T>
class Queue {
    enum {
        SIZE = 8
    };
    T *m_array[SIZE];
    int m_add, m_remove;
public:
    Queue();
    virtual ~Queue();
    void enque(T *c);
    T *deque();
};

#endif /* QUEUE_H_ */

Queue.cpp:

#include "Queue.h"

template<typename T>
Queue<T>::Queue() {
    m_add = m_remove = 0;
}

template<typename T>
Queue<T>::~Queue() {
}

template<typename T>
void Queue<T>::enque(T *c) {
    m_array[m_add] = c;
    m_add = (m_add + 1) % SIZE;
}

template<typename T>
T (Queue<T>::*deque)() {
    int temp = m_remove;
    m_remove = (m_remove + 1) % SIZE;
    return m_array[temp];
}

I am hoping that one of you c++ experts could help me understand how to define the function pointer < T *deque() > Thank you in advance for your help.

Otelagh
  • 13
  • 3

1 Answers1

1

Your implementation doesn't match your declaration (obviously). Your function is declared as:

template<class T>
class Queue
{
    //... other members

    T* deque();
};

Outside of the class body, that becomes:

template<class T>
T* Queue<T>::deque()

That said, you need to read this: "Why can templates only be implemented in the header file". It will explain why it was very likely a mistake that you moved your template implementation to a cpp file from where it belongs: the header.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • thanks for the link. It was helpful. I took your advice and put the implementation in a .tpp file, to be included at the end of the header file. I am still having problems defining the *deque() function however. Question: If you were to define the above *deque() function in a separate .tpp file, how would you do it? I am still mired in compile errors for only this one function ( *deque() ). – Otelagh Aug 20 '16 at 04:52
  • "how would you do it " - *I wouldn't*. I would put the template declaration, and implementation, including the out-of-line implementations, in the header file where they belong. There is no gain *whatsoever* to splitting it into separate header + something-else. Regarding compiling, the syntax for defining that class template member out of class-body is *exactly* as I show it in my answer. If you're still receiving compile time errors, you should add an addendum to your question with the updated source code. – WhozCraig Aug 20 '16 at 05:12
  • ...got it working. I simply needed to "clean" before the compile would work. Thanks, WhozCraig. RE: "There is no gain whatsoever..." I hear what you are saying. I am new to the c++ lang, so I'm trying to establish what works and why. The link you gave me seemed to imply that splitting the template declarations away from the implementations was always good and that using the .tpp file would meet this goal. – Otelagh Aug 20 '16 at 05:37