#include "S_Stack.h"
#include<iostream>
int main()
{
Stack<int,100>i;
int a;
int x;
i.Push(1);
i.Push(2);
i.Push(3);
i.Push(4);
i.Push(5);
std::cout<<i<<std::endl;
a=i.TopItem();
std::cout<<"The top item is "<<a<<std::endl;
i.Pop(x);
std::cout<<i<<std::endl;
if(i.IsEmpty())
{
std::cout<<"Stack is empty"<<std::endl;
}
else
{
std::cout<<"Stack is not empty"<<std::endl;
}
if(i.IsFull())
{
std::cout<<"Stack is full"<<std::endl;
}
else
{
std::cout<<"Stack is not full"<<std::endl;
}
system ("pause");
return 0;
}
if anyone could help me with whatever problems im having i would appreciate it.
Also, share with us what you have done to debug thus far.
By the way, I noticed something peculiar with your destructor. Do not use delete in the destructor. The array was not allocated using operator new. This destructor will cause undefined behavior.
specifically:
32 E:\373\S_Stack.cpp `StackSize' was not declared in this scope
32 E:\373\S_Stack.cpp template argument 2 is invalid
33 E:\373\S_Stack.cpp ISO C++ forbids declaration of `s' with no type
34 E:\373\S_Stack.cpp `Top' has not been declared
34 E:\373\S_Stack.cpp request for member of non-aggregate type before ';' token
35 E:\373\S_Stack.cpp `Items' has not been declared
35 E:\373\S_Stack.cpp request for member of non-aggregate type before '[' token
35 E:\373\S_Stack.cpp At global scope:
38 E:\373\S_Stack.cpp missing `>' to terminate the template argument list
38 E:\373\S_Stack.cpp template argument 2 is invalid
38 E:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
38 E:\373\S_Stack.cpp explicit instantiation of non-template `int StackSize'
38 E:\373\S_Stack.cpp expected `;' before '>' token
40 E:\373\S_Stack.cpp missing `>' to terminate the template argument list
40 E:\373\S_Stack.cpp template argument 2 is invalid
40 E:\373\S_Stack.cpp expected `,' or `...' before '>' token
40 E:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
40 E:\373\S_Stack.cpp template-id `operator<< <>' for `std::ostream& operator<<(std::ostream&, int)' does not match any template declaration
i got the code to output from my professor,so i know its not that.I just dont know if the cause of my problems are in the code or somewhere else in my program.
ok, here is my updated code with significantly less errors
32 F:\373\S_Stack.cpp missing `>' to terminate the template argument list
32 F:\373\S_Stack.cpp template argument 2 is invalid
32 F:\373\S_Stack.cpp expected `,' or `...' before '>' token
32 F:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
33 F:\373\S_Stack.cpp expected unqualified-id before '{' token
33 F:\373\S_Stack.cpp expected `,' or `;' before '{' token
39 F:\373\S_Stack.cpp missing `>' to terminate the template argument list
39 F:\373\S_Stack.cpp template argument 2 is invalid
39 F:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
39 F:\373\S_Stack.cpp explicit instantiation of non-template `int StackSize'
39 F:\373\S_Stack.cpp expected `;' before '>' token
just giving my header and implementation since they're the only stuff that changed
Well, none of us understood the original question. You said that it failed to "output" not that it failed to "compile". We thought that you were expressing problems with running the program. I had pointed out two problems already.
1) Destructor is wrong. There should be no code within it. There is no dynamic memory to delete. (this isn't a compile error but I'm trying to save you from run-time headaches).
2) You still have a typo in the definition of operator<<. One of the "Stacksize" references is mistyped. The name is case sensitive.
1 2
// Change this to template <class t,int StackSize>
template <class t,int Stacksize>
The destructor is meant for deallocating dynamically allocated memory. Using the delete operator calls this function as well. It's anything that needs to be done before the handle to the class (or whatever allocated memory) is set to NULL.
ah ok,my apologizes for the miscommunication on my part.
i made the changes and have less errors now
32 F:\373\S_Stack.cpp missing `>' to terminate the template argument list
32 F:\373\S_Stack.cpp template argument 2 is invalid
32 F:\373\S_Stack.cpp expected `,' or `...' before '>' token
32 F:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
33 F:\373\S_Stack.cpp expected unqualified-id before '{' token
33 F:\373\S_Stack.cpp expected `,' or `;' before '{' token
39 F:\373\S_Stack.cpp missing `>' to terminate the template argument list
39 F:\373\S_Stack.cpp template argument 2 is invalid
39 F:\373\S_Stack.cpp ISO C++ forbids declaration of `StackSize' with no type
39 F:\373\S_Stack.cpp explicit instantiation of non-template `int StackSize'
39 F:\373\S_Stack.cpp expected `;' before '>' token
if someone can just point out where i still have issues i would appreciate it.Im sorry to be a pain like this but i am not a good programmer. thank you.
now im getting:
39 E:\373\S_Stack.cpp `StackSize' was not declared in this scope
39 E:\373\S_Stack.cpp template argument 2 is invalid
40 E:\373\S_Stack.cpp `StackSize' was not declared in this scope
40 E:\373\S_Stack.cpp template argument 2 is invalid
40 E:\373\S_Stack.cpp ISO C++ forbids declaration of `s' with no type
40 E:\373\S_Stack.cpp template-id `operator<< <>' for `std::ostream&
40 E:\373\S_Stack.cpp template-id `operator<< <>' for `std::ostream& operator<<(std::ostream&, int&)' does not match any template declaration
now im getting:
41 E:\373\S_Stack.cpp expected `<' before "ostream"
41 E:\373\S_Stack.cpp expected identifier before '<' token
41 E:\373\S_Stack.cpp expected `,' or `...' before '<' token
41 E:\373\S_Stack.cpp ISO C++ forbids declaration of `parameter' with no type
41 E:\373\S_Stack.cpp expected identifier before '<' token
41 E:\373\S_Stack.cpp expected `,' or `...' before '<' token
41 E:\373\S_Stack.cpp ISO C++ forbids declaration of `parameter' with no type
current line 41 ostream& operator<< (ostream& output, <class t,int StackSize>const &s);