0

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.

cstrike2
  • 55
  • 7
  • 1
    Maybe your prof wants you to put the popped element into the argument, hence the `T&`. – cigien Apr 28 '20 at 03:08
  • 1
    Normally I suggest that you compile and link cpp files and include the .h files, but in this case you have a template implemented in that cpp. file. II recommend renaming it to use a different extension. 1. you will confuse people. 2. you will confuse some build tools. – user4581301 Apr 28 '20 at 03:10
  • 1
    Your function expects a reference argument but you didn't provide one. – eesiraed Apr 28 '20 at 03:45

1 Answers1

0

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thank you! @john That totally was what I wasn't getting. Once I put a char variable in the code it works fine. Had no idea it was supposed to return something. – cstrike2 Apr 28 '20 at 04:29