1

So I've been following a tutorial for making a stack in C++ (here), and I believe I copied his code line for line, but I keep getting this unhandled exception error. I was thinking it had something to do with a pointer, but after scouring my program I can't identify any pointer that is being accessed inappropriately. The exact error message is: "Unhandled exception at 0x00D45446 in Project50.exe: 0xC0000005: Access violation reading location 0x00000014." The program output's the 4th Push, but none of the previous three (what appears on screen, except that the spaces should be dashed lines, but this causes the post to format to bold):

name: Water value: 3

Popping

name:

I'm using visual studio 2012, and the person doing the tutorial is using the Netbeans IDE. Is it possible this is a permissions problem?

Header file:

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

class Stack {
private:
    struct item {
    string name;
    int value;
    item* prev;
    };
    item* stackPtr;
public:
    void Push(string name, int value);
    void Pop();
    void ReadItem(item* r);
    void Print();

    Stack();
    ~Stack();
};

Stack implementation:

#include "Header.h"

using namespace std;

Stack::Stack() {
    stackPtr = NULL;

}

Stack::~Stack() {
    item* p1;
    item* p2;

    p1 = stackPtr;
    while( p1 != NULL) {
        p2 = p1;
        p1 ->prev;
        p2->prev = NULL; // Not actually necessary, but distinguishes that p1 and p2 are pointing to different things
        delete p2;
    }

}

void Stack::Push(string name, int value) {
    item* n = new item;

    n->name = name;
    n->value = value;

    if(stackPtr = NULL) {     // For first item of the stack
        stackPtr = n;
        stackPtr->prev = NULL;  // So that the item at the bottom of the stack points to null 
    }
    else {
        n->prev = stackPtr;
        stackPtr = n;
    }
}

void Stack::ReadItem(item* r) {
    cout << "-------------------------\n";
    cout << "name: " << r->name << endl;
    cout << "value: " << r->value << endl;
    cout << "-------------------------\n";
}

void Stack::Pop() {

    if(stackPtr = NULL) {
        cout << "There is nothing on the stack\n";
    }
    else {
        item* p = stackPtr;
        ReadItem(p);
        stackPtr = stackPtr->prev;
        p->prev = NULL; // Again, like the one in the destructor, not actually necessary.
        delete p;
    }
}

void Stack::Print() {

    item* p = stackPtr;

    while(p != NULL) {
        ReadItem(p);
        p = p->prev;
    }

}

Main:

#include "Header.h"

using namespace std;

int main(int argc, char** argv) {
    Stack Dan;

    Dan.Push("Dan", 3);
    Dan.Push("Coffee", 1);
    Dan.Push("Donuts", 0);
    Dan.Push("Water", 3);
    Dan.Print();

    cout<< "Popping\n";
    Dan.Pop();
    cout<< "Popping\n";
    Dan.Pop();
    cout<< "Popping\n";
    Dan.Pop();
    cout<< "Popping\n";
    Dan.Pop();
    cout<< "Popping\n";
    Dan.Pop();

    cout << '\n';
    system("PAUSE");

    return 0;
}
Dan Minor
  • 85
  • 4
  • 10
  • Your class will instantly fail if I copy an object. See the Rule of Three. – chris Sep 04 '13 at 21:55
  • Don't use `using namespace std;` especially not in a header. – greatwolf Sep 04 '13 at 22:05
  • ReadItem should be private. [btw none of these comments explains the problem] – Dale Wilson Sep 04 '13 at 22:06
  • I don't know why you think that copying code from a YouTube video line for line should never result in an error. This code is horrendous (rule of three broken, for one thing). Learn C++ from [a proper, peer-reviewed book](http://stackoverflow.com/q/388242/560648) instead, please. – Lightness Races in Orbit Sep 04 '13 at 22:15

2 Answers2

2

There is an error here:

void Stack::Pop() {
    if(stackPtr = NULL) {
                ^
                 this should be ==

The same error in Stack::Push.

In the destructor:

Stack::~Stack() {
   item* p1;
   item* p2;

   p1 = stackPtr;
   while( p1 != NULL) {
      p2 = p1;
      p1 ->prev;   // this statement does nothing
2

I'm using visual studio 2012, and the person doing the tutorial is using the Netbeans IDE. Is it possible this is a permissions problem?

No, it's a problem with the person doing the tutorial.

Do not learn C++ from tutorials on YouTube. They are not guaranteed — nay, highly unlikely — to be accurate.

The code mixes = and == and violates the Rule of Three, causing errors on copy. There are further errors and instances of poor style.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • This is probably true, but looking back at the error, using the assignment operator was my own fault. I know I should take a more in depth approach to really get the methodology down, but it just feels so excruciatingly slow to read through most programming book. – Dan Minor Sep 05 '13 at 02:48
  • 2
    @DanMinor: It's _far_ slower to watch a YouTube video and then learn incorrectly. It may feel faster, but it's not. Learning incorrectly is devastating to your immediate and future productivity. Trust me, learning properly from a book is _worth it_; if nothing else, programming is _hard_ and you shouldn't expect to breeze through it in an instant. – Lightness Races in Orbit Sep 05 '13 at 11:46