When I run this code (Visual Studio 2005), I get the following compiler errors and can't fix this no matter what I do:

c:\documents and settings\dthomas006\my documents\visual studio 2005\projects\pa3-q1\pa3-q1\stacktype.h(10) : error C2143: syntax error : missing ';' before '<'

c:\documents and settings\dthomas006\my documents\visual studio 2005\projects\pa3-q1\pa3-q1\stacktype.h(24) : see reference to class template instantiation 'StackType<ItemType>' being compiled

c:\documents and settings\dthomas006\my documents\visual studio 2005\projects\pa3-q1\pa3-q1\stacktype.h(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

c:\documents and settings\dthomas006\my documents\visual studio 2005\projects\pa3-q1\pa3-q1\stacktype.h(10) : error C2238: unexpected token(s) preceding ';'


To me, it sounds like it doesn't like ItemType not being declared but I thought that was the point of using a template. I also attached the file with my main in it. I've looked at other programs I wrote with class templates (this is the first one using stacks) and can't figure out what's wrong. I usually separate the header and implementation files but decided to do it all in one this time. Any suggestions you can give are appreciated. :S

#include <iostream>
#include <cstddef>

using namespace std;

template <class ItemType>
class StackType
{
private:
    NodeType<ItemType>* topPtr; // It points to a singly-linked list
public:
    StackType(); // default constructor: StackType is created and empty
    ~StackType(); // Destructor: memory for nodes needs to be deallocated
    StackType(const StackType<ItemType> &x); // copy constructor: implicitly called for pass
                                    // by value
    void MakeEmpty(); // StackType is made empty
    bool IsEmpty(); // test if the stack is empty
    bool IsFull(); // test if the stack is full
    int Length(); // return the number of elements in the stack
    void Print(); // print the value of all elements 
                    //in the stack in the sequence from the top to bottom
    void Push(ItemType x); // insert x onto the stack- Precondition: the stack is not full
    void Pop(); // delete the top element from the stack -- Precondition: the stack is not empty
};

template<class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};

template<class ItemType>
StackType<ItemType>::StackType()
{
    topPtr=NULL;
}

template<class ItemType>
bool StackType<ItemType>::IsEmpty()
{
    return (topPtr==NULL);
}


template<class ItemType>
StackType<ItemType>::StackType(const StackType<ItemType> &x)
{
    NodeType<ItemType> *p1;
    NodeType<ItemType> *p2;
    
    if (x.topPtr==NULL)
        topPtr=NULL;
    else
    {
        topPtr=new NodeType<ItemType>;
        topPtr->info=x.topPtr->info;
        p1=x.topPtr->next;
        p2=topPtr;

        while (p1 !=NULL)
        {
            p2->next=new NodeType<ItemType>;
            p2=p2->next;
            p2->info=p1->info;
            p1=p1->next;
        }
        p2->NULL;
    }
}

template<class ItemType>
void StackType<ItemType>::MakeEmpty()
{
    NodeType<ItemType> *tp;

    while (topPtr !=NULL)
    {
        temp=topPtr;
        topPtr=topPtr->next;
        delete temp;
    }
}

template<class ItemType>
StackType<ItemType>::~StackType()
{
    NodeType<ItemType> *tp;

    while (topPtr !=NULL)
    {
        temp=topPtr;
        topPtr=topPtr->next;
        delete temp;
    }
}

template<class ItemType>
bool StackType<ItemType>::IsFull( )
{
    NodeType<ItemType> *fp;
    
    if (fp=new NodeType<ItemType>)
    {
        delete fp;
        return false;
    }
    else
        return true;
}


template<class ItemType>
int StackType<ItemType>::Length( )
{
    int count=0;
    NodeType<ItemType> *cp;
    cp=topPtr;

    while (cp!=NULL)
    {
        count++;
        cp=cp->next;
    }
    return count;
}

template<class ItemType>
void StackType<ItemType>::Print( ) // print the value of all elements in the stack in the sequence
// from the top to bottom
{
    NodeType<ItemType> *pp;
    pp=topPtr;

    while (pp!=NULL)
    {
        cout<<pp->info<<endl;
        pp=pp->next;
    }
}


template<class ItemType>
void StackType<ItemType>::Push(ItemType x) // insert x onto the stack
// Precondition: the stack is not full
{
    NodeType<ItemType> *pushptr;
    pushptr = new NodeType<ItemType>;
    pushptr->info=x;
    pushptr->next=topPtr;
    topPtr=pushptr;
}

template<class ItemType>
void StackType<ItemType>::Pop() // delete the top element from the stack
// Precondition: the stack is not empty
{
    NodeType<ItemType> *popptr;
    popptr=topPtr;
    topPtr=topPtr->next;
    delete popptr;
}

NodeType (line 10) has not been defined so your compiler has no clue what it is.

NodeType (line 10) has not been defined so your compiler has no clue what it is.

Thanks much! I now realize before I did a forward declaration then wrote the function definition in the implementation file. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.