0

All of the answers I've seen for this say that this error is usually caused by circular inclusion or improper scope resolution but I can't currently see either one.

"Node" is a simple node class used to build the linked list implemented in "stack." Node.h includes and , and Node.cpp includes "Node.h" and .

I've already narrowed it down to the second to last function in stack.cpp (the last uncommented function). When size(0 is commented out, there are no other errors present.

I apologize in advance for the violated naming scheme. This code is tested by a professor using a driver, so I have to name everything according to our textbook.

stack.h

 #pragma once

    #ifndef STACK_H
    #define STACK_H

    #include <cstdlib>
    #include "Node.h"

    template <class Item>
    class stack {
    public:
        typedef std::size_t size_type;

        stack();
        //Post: stack has been initialized as an empty stack

        void push(const Item& entry);
        //Post: a new copy of entry has been pushed to the stack

        void pop();
        //Pre:  size()>0
        //Post: the item on the top of the stack is removed from the stack

        Item top() const;
        //Pre:  size()>0
        //Post: returns the value of the item on top of the stack but the stack is unchanged

        size_type size() const;
        //Post: returns the number of items in the stack

        bool empty() const;
        //Post: returns true if the stack is empty and false if the stack is not empty

    private:
        Node<Item> *head_ptr;   //top of the stack
                                //tail node is the bottom of the stack
    };
    #endif 

stack.cpp

#include "stack.h"
#include "Node.h"
#include <iostream>

template <class Item> stack<Item>::stack() {
    head_ptr = NULL;
}

template <class Item> void stack<Item>::push(const Item& entry) {
    x = Node();
    x.set_data(entry);
    x->set_link(head_ptr);
    head_ptr = x;
}

template <class Item> void stack<Item>::pop(){
    if (size > 0) {
        Node<Item> *temp = head_ptr->link();
        head_ptr = temp;
    }
}

template <class Item> Item stack<Item>::top() const {
    if (size() > 0)
        return head_ptr->getData();
    std::cout << "The stack is empty, so there is nothing to return." << std::endl;
    return 0;
}

template <class Item> stack<Item>::size_type stack<Item>::size() const { //"Error C2061 syntax error:identifier 'size_type'" occurs here
    count = 0;
    Node<Item> *cursor = head_ptr;
    while (cursor != NULL) {
        count++;
        cursor = cursor->link();
    }
    return count;
}

//template <class Item> bool stack<Item>::empty() const {
//  if (size() > 0)
//      return false;
//  return true;
//}
LLSv2.0
  • 501
  • 6
  • 20
  • You need to use `template typename stack::size_type stack::size() const { ... }`. – R Sahu May 01 '17 at 02:54
  • FYI, nobody memorizes compiler error codes. It's best if you include the *full* error message from the compiler rather than just an error code number. – Cornstalks May 01 '17 at 02:54
  • Also, see [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – R Sahu May 01 '17 at 02:56
  • @RSahu that fixed the problem. Thank you. Why is it that this one needs typename and the other functions don't? – LLSv2.0 May 01 '17 at 03:00
  • @LonnieSouderII, `stack::size_type` is a dependent name. i.e. it depends on the template parameter `Item`. Dependent type names must be used with the `typename` keyword. Please read the answers to the duplicate. – R Sahu May 01 '17 at 03:02

0 Answers0