jordan
jordan

Reputation: 139

BST on c++ error

i tried to create BST on cpp i done the class Node in the class Tree because it's logical. header:

class Node;
class Tree
{
public:
    Node* root;
    Tree();
    Tree(int val);
    void insert(int val);
    class  Node
    {
    public:
        Node();
        Node(int val);
        Node* right;
        Node* left;
        int val;
    };

};

implamation:

Tree::Tree()
{
    this->root = NULL;

}
Tree::Node::Node()
{

}
Tree::Node::Node(int val)
{
    this->val = val;
    this->left = NULL;
    this->right = NULL;
}
void Tree::insert(int val)
{

    this->root = new Node(3);

}

i got an error on

this->root = new Node(3);


IntelliSense: a value of type "Tree::Node *" cannot be assigned to an entity of type "Node *"   
error C2440 : '=' : cannot convert from 'Tree::Node *' to 'Node *'  

what did i done wrong please? root this Node* and new Node (3) return Node* what is the problem? thanks!!!

Upvotes: 0

Views: 38

Answers (1)

Codor
Codor

Reputation: 17605

To my understanding, in your current implementation, you are declaring two classes with the name Node; one is in the global scope while the other one is in the scope of the class Tree. This can be solved by using only one class.

Upvotes: 1

Related Questions