I am writing a C++ program that implements a Binary Search Tree (BST). So, I started off implementing a binary tree node as a struct. Since a binary tree node contains two pointers to other binary tree nodes, the struct includes pointers to variables of that struct type. For this, I understand that a forward declaration of the struct is needed. I’ve done that. Yet, I get a run-time error for the simple program below which does nothing but assign a value to a member of said struct. I don’t understand why. Please let me know the reason.
#include <iostream>
using namespace std;
struct t_node;
struct t_node
{
int data;
t_node* left;
t_node* right;
};
int main()
{
t_node *root;
root->data = 2;
cin.get();
return 0;
}
I am using Dev-C++ 4.9.9.2.
Thanks!
You need to alocate memory for your node. Use
new: