#include "stdafx.h" #include <iostream> using namespace std; struct Node { int Key; Node* Left; Node* Right; }; class Tree { public: Node* Top; Tree() { Top = NULL; } Node* GetTop() { return Top; } void Build(Node* x) { Node* T2 = new Node(); x->Left = T2; x->Left->Key = 2; } }; int main() { Tree T; T.Build(T.GetTop()); cout << T.GetTop()->Left->Key; return 0; } Displays error on line
x->Left = T2; An unhandled exception was raised: write access violation.
x was nullptr.
If there is a handler for this exception, program execution can continue safely.
How to fix?