4

I'm trying to make a priority queue implementation in C++. I have created a header file called 'priority_queue.h' which contains the definition of my class, alongside a template defining a generic type. I also have a file where I implement my method defined in the class definition in a file called 'priority_queue.cpp' where I include 'priority_queue.h'. Both my text editor and compiler are throwing errors, although they seem to be different.

I've looked up other questions on StackOverflow and other sites, I've even straight up copy-pasted some code from online answers that were supposedly correct and even those didn't work.

Here is the code in my 'priority_queue.h' header file

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

template <typename T>

class pqueue{
    public:
        pqueue();
        void insert(T element, int priority);

    private:
        vector<T> elements(); //This vector will store our data in the correct order

};

Here is the code from my 'priority_queue.cpp' file

#include <iostream>
#include <iterator>
#include <vector>
#include "priority_queue.h"

using namespace std;

template <typename T>

void pqueue::insert(T element, int priority){

}

The compiler throws an error and says 'template<class T> class pqueue' used without template parameters.

My text editor gives me an error that says name followed by '::' must be a class or namespace name

I'm on a Dell XPS 13 9370 with the i7 8550U, 16GB of RAM and 256GB SSD running Windows 10 Home Version 1903 and my text editor is Microsoft Visual Studio Code Insiders.

FatherOfGold
  • 58
  • 1
  • 4
  • 3
    "_Name followed by '::' must be class or namespace error even though the '::' is following a class name_" No, it isn't. `pqueue` is a **template** for a class, not a class itself. `pqueue` would be a class. – Algirdas Preidžius Nov 12 '19 at 13:17
  • 1
    ***Here is the code from my 'priority_queue.cpp' file*** Having your template implementations in a `.cpp` file may cause you problems as well. [https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – drescherjm Nov 12 '19 at 13:39

2 Answers2

4

You have to define the member function of the class template in the header. And write

template <typename T>
void pqueue<T>::insert(T element, int priority){

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You can define both the constructor and insert member functions of the template class like this in priority_queue.cpp

template <typename T>
pqueue<T>::pqueue(){}

template <typename T>
void pqueue<T>::insert(T element, int priority){}

To prevent linking errors such as

undefined reference to `pqueue<int>::insert(int, int)'

there are two options

  1. physically move the definition of the template function into the priority_queue.h file, (even if it is not an inline function) as suggested by @Vlad from Moscow

  2. or include the following line template class pqueue<int>; at the end of priority_queue.cpp

Reference https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

nae9on
  • 249
  • 3
  • 9