-2

I am trying to override the operator << for my stack class to make it print all items, plates in this case, but I am getting a segmentation error. I don't know what this means or how to fix it.

  ostream& operator << (ostream &out, Stack &stack){
        int a = stack.getStackCount();
        string outString = "";
        PlateNode temp = PlateNode(stack.getTop().getPlate());
        for(int i =0;i<a;i++){
                outString = outString + ", " + temp.getPlate().getName();
                temp = temp.getNextNode();
        }
        out << outString;
        return out;
}

That is where I tried to override the operator, and my main is:

int main()
{
    Stack nullStack = Stack();
    nullStack.push(pNode);
    nullStack.push("me");
    cout <<"Reached c"<<endl;
    cout<<nullStack;
}

The error was not there before the final line in main was entered. Please Help!

Edit:This is my entire code if that helps, sorry it is not commented fully:

https://docs.google.com/document/d/16pg01muz0S5IMlXBahdL-JyVE5y7buzOVHYEOn_jyvE/edit?usp=sharing

1 Answers1

0

The problem is here:

class Stack
{
private:
  PlateNode *ptrTop;
  int stackCount;
public:
  ...    
  Stack(PlateNode top){
    *ptrTop = top;
    stackCount++;}

You're dereferencing an uninitialized pointer -- trying to assign something to an address that is basically random -- which causes something called undefined behavior, which is a Bad Thing. You probably meant something like this:

ptrTop = top;

Anyway, you should try to get into the habit of developing new functionality (e.g. new classes) in small steps, testing at every step. If you had done that, you would have known the fault was in this method; even if you hadn't been able to spot it at once, you would have been staring at three lines instead of a hundred and fifty.

Beta
  • 96,650
  • 16
  • 149
  • 150
  • This is how I had it originally but then it says "cannot convert plateNode to plateNode* in assignment" and it went away when i put the * by ptrTop, using ptrTop = &top; doesnt work either – Chris Murphy Feb 07 '15 at 04:01
  • actually using: ptrTop = &top; does work but the segmentation fault error is still there – Chris Murphy Feb 07 '15 at 04:03
  • 1
    @ChrisMurphy: I'm not sure exactly what you intended in that constructor, I just know that that code causes undefined behavior, and that assigning a value (of the correct type) to the pointer would not. In general, you can't solve a problem like this by blindly adding and removing '*' and '&', you have to understand what you're trying to do. – Beta Feb 07 '15 at 04:06
  • 1
    @ChrisMurphy Stop playing around with symbols to get around the compiler and actually think about your code. – Red Alert Feb 07 '15 at 04:07
  • Ok sorry, what I thought I was doing was taking the parameter plateNode top and making it the top of the stack. Does it not do that? – Chris Murphy Feb 07 '15 at 04:08
  • @ChrisMurphy You're passing `plateNode top` by value so it's just a copy of the real `plateNode` and gets destroyed by the end of the constructor. – Emil Laine Feb 07 '15 at 04:10
  • so the constructor needs to take *plateNode as a param, is that right? – Chris Murphy Feb 07 '15 at 04:11
  • @ChrisMurphy Preferably `(const) plateNode&` if you're doing C++. I noticed other similar mistakes in your code too. – Emil Laine Feb 07 '15 at 04:12
  • Okay I think most of my errors are concerning passing things by value instead of reference right so I will go study that, thanks!! – Chris Murphy Feb 07 '15 at 04:14
  • @RedAlert wasn't intending to "play around" i thought i was doing it right ive never used c++ before so calm down – Chris Murphy Feb 07 '15 at 04:17
  • @ChrisMurphy Sounds great! It seems most of the parameters in your code get passed as copies so taking the address of those copies wouldn't be making much sense. References should be the preferred way to pass class objects in C++, unless a copy is explicitly needed. – Emil Laine Feb 07 '15 at 04:18