我所看到的所有答案都说这个错误通常是由于循环包含或范围分辨率不合理造成的,但我目前看不到其中任何一个。
"节点"是一个简单的节点类,用于构建在"堆栈中实现的链表。" Node.h包含和,Node.cpp包括" Node.h"和。
我已经将它缩小到stack.cpp中的倒数第二个函数(最后一个未注释的函数)。当大小(0被注释掉时,不存在其他错误。
我提前为违反的命名方案道歉。这段代码由教授使用驱动程序进行测试,所以我必须根据我们的教科书命名所有内容。
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;
//}