Search code examples
c++functionlinked-liststacklifo

Quick Question: Why isn't my call for pop() working? C++ Stacks


I'm learning stacks in my current CS course. I was working on functions and when I went to test, for the pop function I got the error: "no matching function for call to DynStack::pop()"

Here's my code:

main()

#include <iostream>
#include <cstddef>
#include "src\DynStack.cpp"
#include "DynStack.h"

using namespace std;


int main()
{
    DynStack<char>cstack;
    cstack.push('A');
    cstack.push('B');
    cstack.push('C');

    cstack.pop();

    cout << cstack.top->value << endl;

    return 0;
}

DynStack.cpp

#include "DynStack.h"
#include <cstddef>

template<typename T>
DynStack<T>::DynStack()
{
    top = NULL;
}

template<typename T>
void DynStack<T>::push(T val)
{
    StackNode *nodePtr; // ptr to traverse thru the stack

    StackNode *newNode;
    newNode = new StackNode; // makes a new StackNode
    newNode->value = val;
    newNode->next = NULL;

    if (top == NULL) // If the stack is empty
    {
        top = newNode; // Make newNode the first node;
    }
    else
    {
        nodePtr = top; // make our ptr = top

        nodePtr->next = newNode; // make the node after the top our newNode

        top = newNode; // newNode is our new top of the stack
    }
}

template <typename T>
void DynStack<T>::pop(T &)
{
    StackNode *nodePtr; // makes a nodePtr to traverse the stack

    if (top == NULL)
    {
        return;
    }
    else
    {
        nodePtr = top;
        nodePtr = top--;
        delete top;
        top = nodePtr;
        delete nodePtr;
    }

}




template <typename T>
DynStack<T>::~DynStack()
{
    //dtor
}

DynStack.h

#ifndef DYNSTACK_H
#define DYNSTACK_H

template <typename T>
class DynStack
{
    private:
        struct StackNode
        {
            T value;
            StackNode *next;
        };

        StackNode *top;

    public:
        DynStack();
        ~DynStack();
        void push(T);
        void pop(T&);
        bool isEmpty();
};

#endif // DYNSTACK_H

I'm still very early in my testing for this program. I assume I have to put something in the parentheses of cstack.pop();, but I don't know what I'd put there? Pop would just be removing a value yes? I wouldn't think it'd require any input.

If it was my program I'd think to remove the T& from void DynStack::pop(T &), but that was put there by my professor. I imagine he put that there so I'd have to learn this. Googled a bunch but am not getting any closer.

Thanks y'all.


Solution

  • In your code

    void pop(T&);
    

    The T& means that this function expects a (reference) argument of type T (which is char in your case). You didn't provide one so the code doesn't compile. You need to add a char variable to your code

    char x;
    cstack.pop(x);
    cout << "the top item on the stack was " << x << endl;
    

    I think what you are missing is that pop doesn't just remove an item from the stack it also 'returns' that item via the reference arugment.