title: Exception thrown: read access violation. this->top was nullptr
body: <p>I am currently trying to develop a RPM Calculator using C++ but I am running into this error message whenever I try to run my code</p>
  <blockquote>
  <p>Exception thrown: read access violation. this-&gt;top was nullptr.</p>
  </blockquote>
  <p>Here is what I have in my .h file:</p>
  <pre><code>template&lt;class T&gt;
  struct MyStack
  {
      T data; // this is going to store the number
      MyStack&lt;T&gt;* link; // used to point to the next element in the stack
      
  private:
      MyStack&lt;T&gt;* top = NULL; // this will represent the top of the stack when the stack is empty this is set to null
  public:
      void push(T operand);
      void value();
      void pop();
  };
  
  template&lt;class T&gt;
  void MyStack&lt;T&gt;::push(T value) 
  {
      MyStack&lt;T&gt;* ptr = new MyStack&lt;T&gt;; // create a new pointer
      ptr-&gt;data = value;  // set data to whatever the value that is passed in
      ptr-&gt;link = top; 
      top = ptr;
  }
  
  
  template&lt;class T&gt;
  void MyStack&lt;T&gt;::value()
  {
      if (top-&gt;data != NULL) { // here is where the error occurs
          cout &lt;&lt; top-&gt;data &lt;&lt; &quot;&gt;&quot; &lt;&lt; endl;
      }
      else {
          cout &lt;&lt; &quot;X&gt;&quot;;
      }// prints out the current top value
  }
  
  
  template&lt;class T&gt;
  void MyStack&lt;T&gt;::pop()
  {
      MyStack&lt;T&gt;* ptr = top; // creating the pointer
      top = top-&gt;link; //. top is now equal to the link
      delete(ptr); // I can now delete the top value as top is now equal to the link which is the previos value
  }
  </code></pre>
  <p>And here is what I have in my main</p>
  <pre><code>int main() 
  {
      
      MyStack&lt;double&gt; s;
      string input;
  
      while (true) {
          // display prompt
          s.value();
      //    cout &lt;&lt; &quot;VALUE&gt;&quot;;
  
          // get input
          cin &gt;&gt; input;
  
          // check for numeric value
          double num;
          if (istringstream(input) &gt;&gt; num) {
              s.push(num);
          }
          // check for operator
          else if (isOperator(input)) {
              performOp(input, s);
          }
  
          // check for quit
          else if (input == &quot;q&quot;) {
              return 0;
          }
          else {
              cout &lt;&lt; &quot;Invalid Input&quot; &lt;&lt; endl;
          }
      }
      
  
      
  }
  </code></pre>
  <p>Could anyone please help me with this? I have been trying to fix this all day but haven't been able to do it.
  NOTE: I have removed some other functions I had so the code was not too long
  any help will be very much appreciated</p>