0

I'm currently trying to implement Priority queue using Binary search tree. I have the following header file:

template <class T>
class BSTQueue
{
private:
    struct Tree
    {
        T data;
        int priority;
        Tree* lt;
        Tree* rt;
        Tree(T Data, int Priority, Tree* left = nullptr, Tree* right = nullptr)
        {
            data = Data; priority = Priority; lt = left; rt = right;
        }
    };
    Tree* head = nullptr;
    Tree* insert(Tree* current, T data, int priority);
};

The problem is that function insert in .cpp generates errors. My .cpp file is the following:

#pragma once
#include "BSTQueue.h"

template<class T>
BSTQueue<T>::Tree* BSTQueue<T>::insert(Tree* current, T data, int priority)
{
    if (current == nullptr)
    {
        return new Tree(data, priority, nullptr, nullptr);
    }
    if (priority <= current->priority)
    {
        current->lt = insert(current->lt, data, priority);
    }
    else
    {
        current->rt = insert(current->rt, data, priority);
    }
    return current;
}

And the errors:

  • Error C2447 '{': missing function header (old-style formal list?)
  • Error C2143 syntax error: missing ';' before '{'
  • Error C2061 syntax error: identifier 'Tree'

I have no idea what should I do in order to fix them. Maybe someone knows what to do??

  • I've tried this variant, but it still doesn't work and gives same errors – Ваня Самохвалов Dec 27 '21 at 14:33
  • 2
    The reason for closing this question does not seem correct. The problem is not that the function definition is in a separate file (yes, it should be in the header file, but that is an additional comment). The problem is that the "typename" keyword is needed: `template typename BSTQueue::Tree* BSTQueue::insert(BSTQueue::Tree* current, T data, int priority`. – nielsen Dec 27 '21 at 14:41
  • 1
    `#pragma once` shouldn't be in a source file. You also can't implement templates in source files. Also, you'll need to add `typename` before `BSTQueue::Tree` – ChrisMM Dec 27 '21 at 14:43
  • You should not implement your template in a cpp file. If you want to implement it separate file that is included in the header there are several choices: [https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention](https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention) – drescherjm Dec 27 '21 at 14:52
  • Thanks, adding typename worked! – Ваня Самохвалов Dec 27 '21 at 14:52

0 Answers0