Stack Container

I keep getting this error code when trying to compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
In file included from prog6.cpp:10:
Stack.h:21: error: `ItemType' has not been declared
Stack.h:21: error: ISO C++ forbids declaration of `item' with no type
Stack.h:23: error: `ItemType' does not name a type
Stack.h:27: error: `ItemType' does not name a type
prog6.cpp: In function `int main()':
prog6.cpp:39: error: 'class StackType' has no member named 'Top'
In file included from Stack.cpp:11:
Stack.h:21: error: `ItemType' has not been declared
Stack.h:21: error: ISO C++ forbids declaration of `item' with no type
Stack.h:23: error: `ItemType' does not name a type
Stack.h:27: error: `ItemType' does not name a type
 


stack.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    13  class StackType
    14  {
    15
    16  public:
    17
    18  StackType();

    21  void Push(ItemType item);
 
    23  ItemType Top() const;
    24                                                      
    25  private:
 
    27  ItemType items[MAX_ITEMS];
void Push(ItemType item); 
ItemType items[MAX_ITEMS];

The compiler is telling you that is doesn't know what in the world ItemType is.

Maybe you forgot this before your class?

template<typename ItemType>
Last edited on
What do i have missing from this error?

1
2
3
4
5
6
7
8
9
10
11
12
13
In file included from prog6.cpp:10:
Stack.h:28: error: `MAX_ITEMS' was not declared in this scope
prog6.cpp: In function `int main()':
prog6.cpp:20: error: missing template arguments before "stack"
prog6.cpp:20: error: expected `;' before "stack"
prog6.cpp:30: error: `stack' was not declared in this scope
prog6.cpp:34: error: `stack' was not declared in this scope
In file included from Stack.cpp:11:
Stack.h:28: error: `MAX_ITEMS' was not declared in this scope
Stack.cpp:14: error: `template<class ItemType> class StackType' used without template parameters
Stack.cpp:16: error: ISO C++ forbids declaration of `StackType' with no type
Stack.cpp: In function `int StackType()':
 


in main program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    16  int main()
    17  {
    18
    19          char symbol;
    20          StackType stack;
 

    30      stack.Push(symbol);
    31

   
    34    if (stack.IsEmpty())
    35     
    36  
    37  else


in .h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    13  template<typename ItemType>
    14  class StackType
    15  {
    16
    17  public:
    18
    19  StackType();

  
    22  void Push(ItemType item);

    24  ItemType Top() const;
    25  
    26  private:
   
    28  ItemType items[MAX_ITEMS];
    29  
StackType stack;

You need to give it the type of the stack, like this:

StackType<double> stack;

You also have other problems but we'll takle one at a time. Also, stop putting line numbers in, the code tags do that for you.
1
2
3
4
5
In file included from prog6.cpp:2:
Stack.h:6: error: expected constructor, destructor, or type conversion before '<' token
Stack.h:16: error: expected constructor, destructor, or type conversion before '<' token
Stack.h:32: error: expected constructor, destructor, or type conversion before '<' token
Stack.h:39: error: expected initializer before '<' token


Those are the only errors i get when i call the function.

.cpp file
1
2
3
4
5
6
7
8
9
10
      template<class T>
      class StackType
      {
   
      public:
      StackType(int); // constructor
    
      StackType(const StackType &);
      ~StackType();


.h file
1
2
3
4
5
6
7
8
9
10
11
12
       template<class T>
       StackType<T>::StackType(int size)

      template<class T>
      StackType<T>::StackType(const StackType &obj)

      template<class T>
      StackArray<T>::~StackArray()

      template<class T>
      void StackType<T>::Push(T num)
Last edited on
I think you got . and .cpp swapped. In either case, when dealing with templated classes the definitions of the functions need to be in the same file as the definition of the class. Also I think you keep forgetting to copy the }; at the end of your StackType class.

As for the errors, in the code that in your post you marked .h file, you have no body for the functions. Meaning, you forgot the curly braces {}
in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
     9  {
    10
    11          char symbol;
    12          StackType<char> stack;
    13          bool balanced = true;
    14          char openSymbol;
    15
    16    cout << "Enter parentheses and/or braces:" << endl;
    17    cin.get(symbol);
    18
    19  while (symbol != '\n' && balanced)
    20  {
    21    if (IsOpen(symbol))
    22      stack.Push(symbol);
    23


.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
     9  template<class T>
    10  class StackType
    11  {
    12
    13  public:
    14  StackType(int); // constructor
    15
    16  StackType(const StackType &);
    17  ~StackType();
    18
    19  bool IsEmpty() const;
    20  bool IsFull() const;
    21  void Push(T);
    22  void Pop(T &);
    23  
    24  
    25  private:
    26  int top;
    27  T *stackArray;
    28  int stackSize;


.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

     1  #include <iostream>
     2  #include "Stack.h"
     3  using namespace std;
     4

     7  template<class T>
     8  StackType<T>::StackType(int size)
     9  {
    10    stackArray = new T[size];
    11    stackSize = size;
    12          top = -1;
    13  }
    14
    15  template<class T>
    16  StackType<T>::StackType(const StackType &obj)
    17  {
    18  if (obj.stackSize > 0)
    19   stackArray = new T[obj.stackSize];
    20  else
    21   stackArray = NULL;
    22
    23  stackSize = obj.stackSize;
    24  
    25  for (int count = 0; count < stackSize; count++)
    26   stackArray[count] = obj.stackArray[count];
    27  
    28  top = obj.top;
    29  }
    30  
    31  template<class T>
    32  StackArray<T>::~StackArray()
    33  {
    34  delete [] stackArray;
    35  }
    36  
    37  
    38  template<class T>
    39  void StackType<T>::Push(T num)
    40  {
    49  }

    50  
    51  template<class T>
    52  bool StackType<T>::IsEmpty() const
    53  {

    64  }

    65  
    66  template<class T>
    67  bool StackType<T>::IsFull() const
    68  {

    79  }

    88  
    89  template<class T>
    90  void StackType<T>::Pop(T &num)
    91  {
   
   106  }

   114  


i am getting unusual errors
Last edited on
Even if you're not finished, you HAVE to return a value for those functions which return values. Particularly, IsEmpty and IsFull.
Topic archived. No new replies allowed.