类声明后编译错误,Main 不"see"类
Compile error after class declaration, Main doesn't "see" the class
我得到一个错误,涉及下面的类。
#ifndef STACKLL
#define STACKLL
#include <iostream>
using namespace std;
template <class T>
class STACKLL
{
private: struct NODE
{
T info; NODE *next;
};
NODE *Stack;
public: STACKLL()
{Stack = NULL;}
void Push (T x)
{
NODE *p = new (NODE);
p -> info = x;
p -> next = Stack;
Stack = p;
}
T Pop()
{
NODE *p = Stack;
T x = p -> info;
Stack = p -> next;
delete (p);
return x;
}
bool Empty()
{return (Stack == NULL)?true:false;}
};
#endif
这是主程序中使用的类。
#include <iostream>
#include "STACKLL.h"
using namespace std;
int main ()
{
STACKLL <int> s;
int a[5] = {3,9,8,5,7}, sum=0, nodecount=0;
for (int i = 0; i < 5; ++i)
s.Push (a[i]);
while (!s.Empty())
{
int c = s.Pop();
cout << c << "->";
sum += c;
nodecount++;
}
cout << "NULLn";
cout << "Sum of nodes = " << sum << endl;
cout << "There are " << nodecount << " nodesn";
我得到的第一个问题是在类声明的末尾:"错误C2332: 'class':缺少标签名称"。我遇到的第二个问题是"STACKLL s;",main中唯一的堆栈对象。我的编译器读取到STACKLL,但是"<"用红色下划线,显示:"Error: expected an expression"。我以前遇到过这个错误,但上次错误消失了。我想把它固定下来,这样我就不会再遇到这个问题了。
前面定义了一个名为STACKLL
的空宏,然后尝试创建一个名为STACKLL
的类。预处理器将STACKLL
从程序中移除,因此得到如下结果:
class
{
...
这显然是一个语法错误。
第二个问题也是一样的。预处理器删除STACKLL
,所以你得到<int> s;
,这显然也是一个语法错误。
希望这对你有帮助!
亚历克斯
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 函数是否可以访问传递给main()的参数
- 在main()之外初始化std::vector会导致性能下降(多线程)
- 我的 int main() 中出现堆栈溢出错误
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- File.cpp.o:OpenPose 标志 CMakeFiles/.. 的多重定义/main.cpp.o:首先在这里定
- 为什么类和 main() 函数中也有动态内存分配
- 在 main.cpp 的上下文中找不到目录(带有 CMake 的快板)
- 我想在 Main 中用 C++ 调用其他类中的一个类,但我做不到
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 为什么要从main()返回NULL?
- 类声明后编译错误,Main 不"see"类