class stack

I have the code for a class stack. How can I test it? what do I need to put in main?


// stackADT.h
#include <iostream>
#pragma once
using namespace std;
template <class T>
class StackADT{

public:
StackADT();
StackADT(int max);
bool isEmpty();
bool isFull();
void Push(T item);
void Pop();
T Top() ;
~StackADT();

private:
int maxsize;
T* items;
int top;
};

template<class T> StackADT<T>::StackADT()
{
maxsize = 250;
top = -1;
items = new T[maxsize];
}

template<class T> StackADT<T>::StackADT(int max)
{
maxsize = max;
top = -1;
items = new T[maxsize];
}

template<class T> bool StackADT<T>::isFull()
{
return (top == maxsize - 1);
}

template<class T> bool StackADT<T>::isEmpty()
{
return (top== -1);
}

template<class T> StackADT<T>::~StackADT()
{
delete [] items;
}

template<class T> void StackADT<T>::Pop()
{
if (isEmpty())
throw "The stack is Empty!";
top--;
}

template<class T> T StackADT<T>::Top()
{
if (isEmpty())
throw "The stack is Empty!";
return items[top];
}

template<class T> void StackADT<T>::Push( T newItem)
{
if (isFull())
throw "The stack is Full!";
top++;
items[top] = newItem;
}

Create an instance of the class and call the functions you want to test.

I did this, but it gives me 2 erros:
Error 2 error LNK1120: 1 unresolved externals C:\Users\home\Desktop\Asst4\Debug\Asst4.exe 1 1 Asst4
Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup C:\Users\home\Desktop\Asst4\MSVCRTD.lib(crtexe.obj) Asst4


#include <iostream>
#pragma once
using namespace std;
template <class T>
int main ()
{

cout << "Test 1"<< endl;


return 0;
}
Remove #pragma once and template <class T> from above the main function.
thank you. it works.:)
still triyng to test it, getting error. any ideas why?

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

int main (void)
{
int n =0;
StackADT stack;//error: aggrument list for class template is missing
if (!stack.isFull ())
stack.Push (2);
while (!stack.isEmpty ())
{
n= stack.Top ();
stack.Pop ();
cout << n << endl;
}
return 0;
}
You need to specify the template parameter type.
If you want the stack to store integers you write StackADT<int> stack;
from what I read from your code:
1
2
3
4
5
6
//this:
StackADT stack;//error: aggrument list for class template is missing

//needs to be:
StackADT<someType> stack;
Last edited on
thank you guys:)
Topic archived. No new replies allowed.