generic stack with a static array

Pages: 12
Im trying to get a stack with a static array. Most of it works except for when im trying to output.

Header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef S_Stack_h_
#define S_Stack_h_
#include <iostream>
template<class StackItem,int StackSize>
class Stack
{
      template <typename t>      
      friend std::ostream& operator<<(std::ostream & , const Stack<t,StackSize>&);  
      private:
             StackItem Items[StackSize];
             int Top;
      public:
             Stack(){Top=0;}
             void Push(StackItem);
             void Pop(StackItem &);
             StackItem TopItem();
             bool IsEmpty(){return (Top==0);}
             bool IsFull () {return (Top==StackSize);}
             void clear();
             ~Stack();
};
#endif 


implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "S_Stack.h"
#include<iostream>
using namespace std;
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem,int StackSize>
StackItem Stack<StackItem,StackSize>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem,int StackSize>
void Stack<StackItem,StackSize>::clear()
{
     Top=0;
}
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
template <class t,int Stacksize> //problem function
ostream& operator<<(ostream &output,const Stack<t,StackSize> &s)
{          
           for(int i=0; i<s.Top; i++)
                   output<<s.Items[i]<<"|";
           return output;
}
template class Stack<int,int StackSize>;

template ostream& operator<< (ostream &output, const Stack<int,int StackSize> &s);


main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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.
What is happening when you output it? Is the data not showing up or what?
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.

1
2
3
4
5
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
Last edited on
Im just getting errors mostly with the function/lines.

1
2
3
4
5
6
7
8
9
10
11
template <class t,int Stacksize> 
ostream& operator<<(ostream &output,const Stack<t,StackSize> &s)
{          
           for(int i=0; i<s.Top; i++)
                   output<<s.Items[i]<<"|";
           return output;
}
template class Stack<int,int StackSize>;

template ostream& operator<< (ostream &output, const Stack<int,int StackSize> &s);


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.
Last edited on
By the way, what you have posted won't compile. Notice the case issue with the stacksize typename.
1
2
3
4
5
6
7
template <class t,int Stacksize> //problem function
ostream& operator<<(ostream &output,const Stack<t,StackSize> &s)
{          
           for(int i=0; i<s.Top; i++)
                   output<<s.Items[i]<<"|";
           return output;
}
Last edited on
anyway you could be a bit more specific? I am aware that it wont compile. Am i missing something,spell something wrong or what?
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

header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef S_Stack_h_
#define S_Stack_h_
#include <iostream>
template<class StackItem,int StackSize>
class Stack
{
      template <typename t>      
      friend std::ostream& operator<<(std::ostream & ,const Stack<t,StackSize>&);  
      private:
             StackItem Items[StackSize];
             int Top;
      public:
             Stack(){Top=0;}
             void Push(StackItem);
             void Pop(StackItem &);
             StackItem TopItem();
             bool IsEmpty(){return (Top==0);}
             bool IsFull () {return (Top==StackSize);}
             void clear();
             ~Stack();
};
#endif 


implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "S_Stack.h"
#include<iostream>
using namespace std;
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem,int StackSize>
StackItem Stack<StackItem,StackSize>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem,int StackSize>
void Stack<StackItem,StackSize>::clear()
{
     Top=0;
}
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
template <class t,int Stacksize> 
ostream& operator<<(ostream &output,Stack<t,int StackSize> const &s);
{        
           typename<t,StackSize>;
           for(int i=0; i< s.Top; i++)
                   output<<s.Items[StackSize]<<"|";
           return output;
}
template class Stack<int,int StackSize>

template ostream& operator<< (ostream &output,const Stack<int,int StackSize>const &s);


any help is appreciated
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> 
closed account (S6k9GNh0)
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


1
2
3
4
5
6
7
8
9
10
11
template <class t,int StackSize> 
ostream& operator<<(ostream &output,const Stack<t,int StackSize> const &s);
{        
           typename<t,StackSize>;
           for(int i=0; i< s.Top; i++)
                   output<<s.Items[StackSize]<<"|";
           return output;
}
template class Stack<int,int StackSize>

template ostream& operator<< (ostream &output,const Stack<int,int StackSize>const &s);


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.
It would help if the line numbers matched up...
here you go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "S_Stack.h"
#include<iostream>
using namespace std;
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem,int StackSize>
StackItem Stack<StackItem,StackSize>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem,int StackSize>
void Stack<StackItem,StackSize>::clear()
{
     Top=0;
}
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
template <class t,int StackSize> 
ostream& operator<<(ostream &output,const Stack<t,int StackSize> const &s);
{        
           typename<t,StackSize>;
           for(int i=0; i< s.Top; i++)
                   output<<s.Items[StackSize]<<"|";
           return output;
}
template class Stack<int,int StackSize>

template ostream& operator<< (ostream &output,const Stack<int,int StackSize>const &s);
On line 32, you have Stack<t, int StackSize>...I think you meant Stack<t, StackSize>?
cool got that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "S_Stack.h"
#include<iostream>
using namespace std;
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem,int StackSize>
StackItem Stack<StackItem,StackSize>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem,int StackSize>
void Stack<StackItem,StackSize>::clear()
{
     Top=0;
}
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
template <class t,int StackSize> 
ostream& operator<< (ostream &output, Stack<t,StackSize> const &s)
{        
           for(int i=0; i< s.Top; i++)
                   output<<s.Items[StackSize]<<"|";
           return output;
}

template class Stack<int,StackSize>;
template ostream& operator<< (ostream &output,Stack<int,StackSize>const &s);


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

any help is appreciated
On line 39, you want to make the template the same as like 31.
got that

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "S_Stack.h"
#include<iostream>
using namespace std;
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem,int StackSize>
void Stack<StackItem,StackSize>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem,int StackSize>
StackItem Stack<StackItem,StackSize>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem,int StackSize>
void Stack<StackItem,StackSize>::clear()
{
     Top=0;
}
template<class StackItem,int StackSize>
Stack<StackItem,StackSize>::~Stack()
{
   delete []Items;
}
template <class t,int StackSize> 
ostream& operator<< (ostream &output, Stack<t,StackSize> const &s)
{        
           for(int i=0; i< s.Top; i++)
                   output<<s.Items[StackSize]<<"|";
           return output;

}

template <class t,int StackSize>
template ostream& operator<< (ostream& output,<class t,int StackSize>const &s);


I appreciate the help so far,i just need help with these last few errors.
You have an extra template before the function on line 41.
got that

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);

thanks for the help on this
Last edited on
Remove class and int in the line (they are already declared in the template above it).
Just a thought, but shouldn't line 32 be declared to be a member of Stack?
Pages: 12