在课堂声明后编译错误,主要没有""见"班级

时间:2014-03-13 22:46:34

标签: c++ class compiler-errors

我收到涉及下面课程的错误。

#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 << "NULL\n";
    cout << "Sum of nodes = " << sum << endl;
    cout << "There are " << nodecount << " nodes\n";

我得到的第一个问题是在类声明结束时:“错误C2332:'class':缺少标记名”。 我得到的第二个问题是“STACKLL s;”,这是主要的唯一堆栈对象。我的编译器读取到STACKLL但是“&lt;”用红色加下划线,说:“错误:预期表达式”。我之前遇到过这个错误,但上次错误消失了。我想把它固定下来,所以我再也不会遇到这个问题了。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:6)

您之前定义了一个名为STACKLL的空宏,然后尝试创建一个名为STACKLL的类。预处理器从您的程序中删除STACKLL,所以你得到了这个:

class
{
    ...

这显然是语法错误。

对于第二个问题,它是一回事。预处理器删除STACKLL,因此您得到<int> s;,这显然也是语法错误。

希望这有帮助!

-Alex