-2

I'm trying to create a function that is going to return a struct I made, when I try to define the function the the node I'm trying to reference returns an error saying "identifier is undefined".

template <class T>
class SplayTree
{
private:
    struct splayNode
    {
        T element;
        splayNode *leftChild;
        splayNode *rightChild;
        size_t height;

        splayNode(T ele, splayNode left, splayNode right, size_t h = 0)
            : element{ele}, leftChild{left}, rightChild{right}, height { h }
    };

    splayNode * root;

    void rotateLeft(splayNode * node);
    void rotateRight(splayNode * node);
    splayNode splay(splayNode * root, T element);


public:
    void insert(T element);
    void remove(T element);
    bool find(T element);
    std::vector<T> preOrderWalk() const;
    std::vector<T> inOrderWalk() const;
    std::vector<T> postOrderWalk() const;
    T getMin();
    T getMax();
    T getRoot();
    size_t size();

};

and the definition:

template <class T>
splayNode SplayTree<T>::splay(splayNode * node, T element)
{
    if (root == nullptr || root->key == key)
    {
        return root;
    }

    if (key < root->key)
    {
        if (root->left == nullptr)
        {
            return root;
        }
    }
}

I've checked multiple other questions like this but its mostly code structure. But I'm pretty sure I structured this correctly.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – πάντα ῥεῖ Jun 27 '19 at 16:07
  • Out of curiosity, does that constructor for `SplayTree::splayNode` actually have a *body*, because as-written this stands no chance to even compile past that. IF it does, it should be in your posted code. And fwiw, as-written the return type for the implementation of `splay` should be something like `typename SplayTree::splayNode` Unrelated, there are clear and present code paths out of `splay` that *don't* return *anything*, thereby not conforming to the promise to do so. – WhozCraig Jun 27 '19 at 16:18

1 Answers1

1

Because splayNode has been defined inside the class SplayTree you need to refer to it as

SplayTree::splayNode

Outside of the scope of the class. Hope this helps.