0

I've got this in the header file for a templatized priority queue;

template <typename ElemType>
class HeapPQueue {
public:
    HeapPQueue<ElemType>();
    ~HeapPQueue<ElemType>();

etc

And this in the cpp file;

#include "pqueue-heap.h"
#include "cmpfn.h"
#include "error.h"
using namespace std;

template <typename ElemType>
HeapPQueue<ElemType>::HeapPQueue() {
    capacity = START_CAPACITY;
    heap = new ElemType[capacity];
    logSize = 0;
}

template <typename ElemType>
HeapPQueue<ElemType>::~HeapPQueue() {
    delete[] heap;

}

I'm getting the error message "unknown type name HeapPQueue". Why?

Robbie Cooper
  • 483
  • 2
  • 5
  • 13
  • Don't put generic template definitions in a different file. – chris Jun 09 '13 at 19:07
  • 1
    Are you doing this for fun or as an assignment? If not, have a look at `std::priority_queue`. – juanchopanza Jun 09 '13 at 19:08
  • Is the header file called `pqueue-heap.h`? – Vaughn Cato Jun 09 '13 at 19:09
  • That question was asked 4 years ago. I'm following the instructions not only in my course reader but also in other books I've got. Which bit do I take out of the cpp- the template ? – Robbie Cooper Jun 09 '13 at 19:10
  • yes it is called pqueue-heap.h – Robbie Cooper Jun 09 '13 at 19:10
  • Do you have any preprocessor directives that might be causing HeapPQueue not to be defined? Any `#ifdef`? – Vaughn Cato Jun 09 '13 at 19:11
  • Did you end your class in the header with `};`? While it is true that you should put your template functions into the header (and when trying to use your queue you'll get tons of errors related to this), the specific error you mentioned should not be caused by this. But forgetting the semicolon at the end of the class definition is a common error that can lead to very strange errors messages. – celtschk Jun 09 '13 at 19:12
  • #ifndef __pathfinder__pqueue__heap__ – Robbie Cooper Jun 09 '13 at 19:13
  • Is the cpp file included in the header file? – Vaughn Cato Jun 09 '13 at 19:14
  • While it's *probably* not related to the problem you see, you should never use double underscores. All identifiers with double underscores are reserved for the implementation. – celtschk Jun 09 '13 at 19:14
  • I've got the semi colon in the header file. – Robbie Cooper Jun 09 '13 at 19:15
  • BTW, what is *at the exact line* where you get the error message? – celtschk Jun 09 '13 at 19:16
  • The cpp #include is in the header file. The #ifndef was generated automatically when I created the header... – Robbie Cooper Jun 09 '13 at 19:17
  • Th error message is on the line HeapPQueue::HeapPQueue() { – Robbie Cooper Jun 09 '13 at 19:18
  • You don't want files that include each other. This can cause an unexpected ordering of how the files are processed. Try removing the `#include "pqueue-heap.h"` from the source file. Make sure the cpp file is included at the *bottom* of the header file, and make sure nothing includes the cpp file. – Vaughn Cato Jun 09 '13 at 19:19
  • Ok, if it's simply a matter of putting the functions into the header, I'll do that! – Robbie Cooper Jun 09 '13 at 19:20
  • Also make sure that cpp file is not compiled. Using a `tpp` extension, or something like that to distinguish it from a regular source file would be better. – Vaughn Cato Jun 09 '13 at 19:21

0 Answers0