我收到未声明的标识符错误,但我已包含头文件?

I am getting an undeclared identifier error but I have included the header file?

本文关键字:包含头 文件 错误 未声明 标识符      更新时间:2023-10-16

我正在尝试创建一个程序来帮助我更好地理解堆栈,但在尝试将模板实现到我的头文件中时遇到了此错误。它说的是一堆未声明的标识符错误,以及尝试在 main.cpp 中创建新对象时意外的字符。

我已经尝试过确保头文件正常工作,所以一个空白的主文件.cpp并且只包含头文件,它像那样编译,但是当我尝试从头文件类创建新对象时,我得到错误。我花了一些时间在网上试图找到类似的东西,但他们似乎都非常明确地解决了他们的问题,这对我并没有太大帮助。

这是我头文件的一部分:

#ifndef STACKASLLIST_H
#define STACKASLLIST_H
using namespace std;
template<typename T> class StackAsLList
{
private:
struct StackNode
{
T ch;
StackNode<T> *next;
};
StackNode<T> *top;
public:
StackAsLList();     
void ClearStack();  /// Remove all items from the stack
void Push(T ch);    /// Push an item onto the stack
T Pop();            /// Pop an item from the stack
bool isEmpty();     /// Return true if stack is empty
bool isFull();      /// Return true if stack is full
~StackAsLList() /// Class destructor
{
ClearStack();
}
};

这是我的主要.cpp我尝试实现头文件的地方:

#include "StackAsLList.h"
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
//this is the line where I start getting errors
StackAsLList<char>*theStack = new <char>StackAsLList();
}

我希望能够运行该程序并使用我在类中创建的函数,但我不断收到未声明的标识符错误。任何帮助将不胜感激!

更改

#include "StackAsLList.h"
#include "pch.h"

#include "pch.h"
#include "StackAsLList.h"