0

I want to create a factory, that returns AVLNode, if BinaryTree is AVLTree, and Node if the tree is not AVL. I have following code:

#include "BinaryTree.h"
#include "AVLTree.h"

class NodeFactory {

public :
    template <class T>
    static Node<T>* getNode(BinaryTree<T>* tree);
};

template <class T>
Node<T>* NodeFactory::getNode(BinaryTree<T>* tree) {
    if (tree->isAVL()) {
        return new AVLNode<T>();
    } else {
        return new Node<T>();
    }
}

UPD: (this is BinaryTree.h) 
template <class T> class Node;

template <class T> class BinaryTree {

public:

BinaryTree() {
    _isAVL = false;
    root = new Node<T>();
}

bool isAVL() {
    return _isAVL;
}

private:
    T elem;
    Node<T>* root;
    bool _isAVL;
};

template <class T> class Node {

public:
    Node() {
        left = NULL;
        right = NULL;
    }

    T get() {
        return elem;
    }

    void setRight(const T elem) {
        right = new Node<T>();
        right->set(elem);
    }

    void setLeft(const T elem) {
        left = new Node<T>();
        left->set(elem);
    }

private:
        T elem;
        Node* left;
        Node* right;
};

I removed almost all methods to make code more readable.

Now i have this error during compilation: "expected initializer before '<' token". Also Qt do not highlights in Node, but highlights in BinaryTree

3
  • What is the question? Commented Nov 27, 2013 at 19:32
  • Oh, i deleted question while editing Commented Nov 27, 2013 at 20:55
  • "expected initializer before '<' token" in what line? Commented Nov 27, 2013 at 21:16

1 Answer 1

1

It's a syntax error. You need

template <class T> // or <typename T>
Node<T>* NodeFactory::getNode(BinaryTree<T>* tree) {
Sign up to request clarification or add additional context in comments.

5 Comments

So just typename instead of class?
typename and class can be used interchangebly (unless you've got template template parameter). You're missing the whole template specifier.
Hmm, now compiler tells me that expected initializer before '<' token. And Qt do not highlights <T> in Node<T>, but highlights <T> in BinaryTree<T>
@SemyonDanilov That's got something to do with definitions of Node or BinaryTree, probably. Can't help more without those.
@jrok i just added them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.