Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,571 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,658 people online right now. Registration is fast and FREE... Join Now!




Help on stack project please

 
Reply to this topicStart new topic

Help on stack project please, errors on implementation file

visionz
28 Oct, 2007 - 09:29 PM
Post #1

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 17


My Contributions
I have to make a code for a stack in c++ this is what i have and im getting a few errors on my implementation.

i haven't made my main yet because i wanted to clear all my implementation errors before i started the main.

Header
CODE

template <class Type>
class stackType
{
    public:
        void initializeStack();
        bool isEmptyStack() const;
        bool isFullStack() const;
        void destroyStack();
        void push(const Type& newItem);
        Type top() const;
        void pop();
        stackType(int stackSize = 100);
        ~stackType();

    private:
        int maxStackSize;
        int stackTop;
        Type *list;
};



Implementation
CODE

#include "stackADT.h"
#include <iostream>
using namespace std;

template <class Type>
void stackType::initializeStack()
{
    stackTop = 0;
}

template <class Type>
bool stackType::isEmptyStack() const
{
    return(stackTop == 0);
}

template <class Type>
bool stackType::isFullStack() const
{
    return )stackTop == maxStackSize);
}

template <class Type>
void stackType::destroyStack()
{
    stackTop = 0;
}

template <class Type>
void stackType::push(const Type& newItem)
{
    if (!isFullStack())
    {
        list[stackTop] = newItem;
        stackTop++;
    }

    else
        cout<<"Cannot add to a full stack."<<endl;
}

template <class Type>
Type stackType::top() const
{
    assert(stackTop != 0);
    return list[stackTop - 1];
}

template <class Type>
void stackType::pop()
{
    if (!isEmptyStack())
        stackTop--;
    else
        cout<<"Cannot remove from an empty stack."<<endl;
}

template <class Type>
stackType::stackType(int stackSize)
{
    if (stackSize<=0)
    {
        cout<<"Size of the array to hold the stack must be positive."<<endl;
        cout<<"Creating an array of size 100."<<endl;
        
        maxStackSize = 100;
    }
    else
        maxStackSize = stackSize;
    stackTop = 0;
    list = new Type[maxStackSize];
}

template <class Type>
stackType::~stackType()
{
    delete [] list;
}


Errors
CODE

error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(19) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(25) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(31) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(37) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(50) : error C2955: 'stackType' : use of class template requires template argument liststackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(57) : error C2955: 'stackType' : use of class template requires template argument liststackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(66) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(82) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
Error executing cl.exe.

stackADTImplementation.obj - 9 error(s), 0 warning(s)


anybody know where i went wrong?

This post has been edited by visionz: 28 Oct, 2007 - 09:38 PM
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Help On Stack Project Please
28 Oct, 2007 - 09:41 PM
Post #2

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
You have done the declaration part correctly but the definition should be done with class reference name

eg:

int classname :: function()
{
}



Or


You can define your functions inside the class rather seperately.
When you define template class for classes it is expected while creating an object we need to specify the type before instantiating an object.
eg

StackType <int> a;

So are the compiler errors refering to...


The logic is correct go furthur and write the main function that should solve the prob.

This post has been edited by harshakirans: 28 Oct, 2007 - 09:53 PM
User is offlineProfile CardPM
+Quote Post

visionz
RE: Help On Stack Project Please
28 Oct, 2007 - 09:43 PM
Post #3

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 17


My Contributions
wow quick to reply. well ya about a min after i posted this i noticed that too. so i changed it and edited my new errors and new implementation thinking nobody saw my post yet. mind taking another look?
User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Help On Stack Project Please
28 Oct, 2007 - 09:55 PM
Post #4

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
The logic is fine go on and write the main function and then report the compiler errors..
User is offlineProfile CardPM
+Quote Post

visionz
RE: Help On Stack Project Please
28 Oct, 2007 - 10:09 PM
Post #5

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 17


My Contributions
so this is my main.

CODE

#include "stackADT.h"
#include <iostream>
using namespace std;

int main()
{
    stackType<int> stack(50);
    stack.initializeStack();
    stack.push(23);
    stack.push(45);
    stack.push(38);
    cout<<"The elements of stack: ";
    while (!stack.isEmptyStack())
    {
        cout<<stack.top()<<" ";
        stack.pop();
    }
    cout<<endl;

    if(!stack.isEmptyStack())
        cout<<"The original stack is not empty."<<endl;
        cout<<"The top element of the stack: "<<stack.top()<<endl;

        return 0;
}

it builds 0 errors and 0 warnings.

but when it comes to becoming compiled im getting the same errors from before. it jumps to the implementation and gives me this

CODE

error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(19) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(25) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(31) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(37) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(50) : error C2955: 'stackType' : use of class template requires template argument liststackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(57) : error C2955: 'stackType' : use of class template requires template argument liststackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(66) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
stackADTImplementation.cpp(82) : error C2955: 'stackType' : use of class template requires template argument list
stackadt.h(35) : see declaration of 'stackType'
Error executing cl.exe.

stackADTImplementation.obj - 9 error(s), 0 warning(s)


User is offlineProfile CardPM
+Quote Post

harshakirans
RE: Help On Stack Project Please
28 Oct, 2007 - 11:22 PM
Post #6

D.I.C Head
Group Icon

Joined: 26 Apr, 2006
Posts: 122



Thanked: 3 times
Dream Kudos: 150
My Contributions
when you instantiate a object of class template you need to declare the constructor as below

Class templates and member function templates are different for reference
http://www.devarticles.com/c/a/Cplusplus/A...us-Templates/1/
this link may help you
CODE

template <class T>

public class stackType
{
void stackType(T &x)::initializeStack()
{
    stackTop = 0;
}

...


};

User is offlineProfile CardPM
+Quote Post

visionz
RE: Help On Stack Project Please
29 Oct, 2007 - 09:53 AM
Post #7

New D.I.C Head
*

Joined: 15 Oct, 2007
Posts: 17


My Contributions
not quite sure if anybody will get to this on time because i have class in a couple hours.but ill give it a try

thnx for the help, but now i have a NEW compile error. ive had this compile error before but not for the same reason. For my last program i forgot to link my implementation with my main. but i believe im getting the error now due to something wrong with my templates.

Header
CODE

template <class Type>
class stackType
{
    public:    
        void initializeStack(); //Function to initialize the stack to an empty state.
        bool isEmptyStack() const; //Function to determine whether the stack is empty.
        bool isFullStack() const;//Function to determine whether the stack is full
        void destroyStack(); //Function to destroy current stack
        void push(const Type& newItem); //Functions to add newItem to the stack
        Type top() const; //Function to return the top element of the stack
        void pop(); // Function to remove the top element of the stack
        stackType(int stackSize = 25); //constructor
        ~stackType(); //destructor
    private:
        int maxStackSize; //variable to store the maximum stack size
        int stackTop; //variable to point to the top of the stack
        Type *list; //pointer to the array that holds the stack elements
};



implementation
CODE

#include "stackADT.h"
#include <iostream>
using namespace std;

template <class Type>
void stackType<Type>::initializeStack()
//Function to initialize the stack to an empty state.
//Precondition:
//Postcondition: stackTop= 0
{
    stackTop = 0;
}

template <class Type>
bool stackType<Type>::isEmptyStack() const
//Function to determine whether the stack is empty.
//Precondition:
//Postcondition:Returns true if the stack is empty, otherwise
//                returns false
{
    return(stackTop == 0);
}

template <class Type>
bool stackType<Type>::isFullStack() const
//Function to determine whether the stack is full
//Precondition:
//Postcondition:Returns true if the stack is full, otherwise
//                returns false
{
    return )stackTop == maxStackSize);
}

template <class Type>
void stackType<Type>::destroyStack()
//Function to destroy current stack
//Precondition:
//Postcondition:
{
    stackTop = 0;
}

template <class Type>
void stackType<Type>::push(const Type& newItem)
//Functions to add newItem to the stack
//Precondition: The stack exists and is not full
//Postcondition: The stack is changed and newItem
//                 is added to the top of the stack
{
    if (!isFullStack())
    {
        list[stackTop] = newItem;
        stackTop++;
    }

    else
        cout<<"Cannot add to a full stack."<<endl;
}

template <class Type>
Type stackType<Type>::top() const
//Function to return the top element of the stack
//Precondition: The stack exists and is not empty
//Postcondition: If stack is empty, program terminates
//                 , otherwise top element of stack is returned
{
    assert(stackTop != 0);
    return list[stackTop - 1];
}

template <class Type>
void stackType<Type>::pop()
// Function to remove the top element of the stack
//Precondition: Stack exists and is not empty
//Postcondition: Stack is changed and top element is
//                 removed from stack
{
    if (!isEmptyStack())
        stackTop--;
    else
        cout<<"Cannot remove from an empty stack."<<endl;
}

template <class Type>
stackType<Type>::stackType(int stackSize)
//constructor
//Postcondition: Variable list contains base address
//                 of array, stacktop = 0, maxStackSize = stackSize.
{
    if (stackSize<=0)
    {
        cout<<"Size of the array to hold the stack must be positive."<<endl;
        cout<<"Creating an array of size 25."<<endl;
        
        maxStackSize = 25;
    }
    else
        maxStackSize = stackSize;
    stackTop = 0;
    list = new Type[maxStackSize];
}

template <class Type>
stackType<Type>::~stackType()
//destructor
//Postcondition: the array (list) holding the stack elements is deleted.
{
    delete [] list;
}


Main
CODE

#include "stackADT.h"
#include <iostream>
using namespace std;

int main()
{
    stackType<int> stack(25); //declare stack size
    stack.initializeStack(); //creates empty stack
    stack.push(23); //add 23 to stack
    stack.push(45); //add 45 to stack
    stack.push(38); //add 38 to stack
    cout<<"The elements of stack: "; //display elements of stack
    while (!stack.isEmptyStack())
    {
        cout<<stack.top()<<" ";
        stack.pop();
    }
    cout<<endl;

    if(!stack.isEmptyStack())
        cout<<"The original stack is not empty."<<endl;
        cout<<"The top element of the stack: "<<stack.top()<<endl;

        return 0;
}


this is the error im getting, i heard its a common mistake to get these lnk2001 errors with templates
CODE

tackADTmain.obj : error LNK2001: unresolved external symbol "public: __thiscall stackType<int>::~stackType<int>(void)" (??1?$stackType@H@@QAE@XZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::pop(void)" (?pop@?$stackType@H@@QAEXXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: int __thiscall stackType<int>::top(void)const " (?top@?$stackType@H@@QBEHXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: bool __thiscall stackType<int>::isEmptyStack(void)const " (?isEmptyStack@?$stackType@H@@QBE_NXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::push(int const &)" (?push@?$stackType@H@@QAEXABH@Z)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: void __thiscall stackType<int>::initializeStack(void)" (?initializeStack@?$stackType@H@@QAEXXZ)
stackADTmain.obj : error LNK2001: unresolved external symbol "public: __thiscall stackType<int>::stackType<int>(int)" (??0?$stackType@H@@QAE@H@Z)
Debug/stackADTImplementation.exe : fatal error LNK1120: 7 unresolved externals


This post has been edited by visionz: 29 Oct, 2007 - 09:54 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Help On Stack Project Please
29 Oct, 2007 - 10:54 AM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,277



Thanked: 135 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Sometimes template classes need everything in one file, not the typical .h and .cpp file pair. Try putting them together and see how it goes.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:24PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month