user3417685
user3417685

Reputation: 3

Compile error after class declaration, Main doesn't "see" the class

I'm getting an error involving the class below.

#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

Here's the class being used in the main program.

#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";

The first problem I'm getting is at the end of the class declaration :"error C2332: 'class' : missing tag name". The second problem I'm getting is at "STACKLL s;", the only stack object in the main. My compiler reads up to STACKLL but the "<" is underlined with the red, saying: "Error: expected an expression". I've come across this error before, but last time the error simply disappeared. I want to pin this down so I won't get this problem ever again. Any help would be greatly appreciated.

Upvotes: 0

Views: 242

Answers (1)

Alex
Alex

Reputation: 699

You previously define an empty macro called STACKLL, then you try to create a class called STACKLL. The preprocessor removes STACKLL from your program, so you get this:

class
{
    ...

This is obviously a syntax error.

With the second problem, it's the same thing. The preprocessor deletes STACKLL, so you get <int> s;, which is also obviously a syntax error.

Hope this helps!

-Alex

Upvotes: 6

Related Questions