Error with template class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED
#include "NumStack.hpp"
 template <class T> class Stack : public NumStack
{
public:
   // Constructor
   Stack(T s) : NumStack(s) {}

   // MathStack operations
   void add();
   void sub();
   void mult();
   void div();
   void addAll();
   void multAll();
};

#endif // STACK_HPP_INCLUDED 


On line 5 I'm getting the error "expected class-name before '{' token". Could someone explain what I'm doing wrong?
Are there any other error messages? Please post the complete error message, all of them, exactly as they appear in your development environment.

How is NumStack defined?

is NumStack also template class/
NumStack is also a template class.
NumStack.hpp
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
#ifndef NUMSTACK_HPP_INCLUDED
#define NUMSTACK_HPP_INCLUDED
template <class T> class NumStack
{
private:
   T *stackArray;  // Pointer to the stack array
   T stackSize;    // The stack size
   T top;          // Indicates the top of the stack

public:
   // Constructor
   NumStack(T);

   // Copy constructor
   NumStack(const NumStack &);

   // Destructor
   ~NumStack();

   // Stack operations
   void push(T);
   void pop(T &);
   bool isFull() const;
   bool isEmpty() const;
};

#endif // NUMSTACK_HPP_INCLUDED 


I do have a couple other errors. In the Stack<T> constructor I'm getting the error
"class 'Stack<T> does not have any field named 'NumStack'" In main it also says that I'm missing template arguments before 'stack'.
Here is my 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// This program demonstrates the MathStack class.
#include <iostream>
#include "stack.hpp"



int main()
{
   int catchVar;  // To hold values popped off the stack

   // Create a MathStack object.
   Stack stack(5);

   // Push 3 and 6 onto the stack.
   std::cout << "Pushing 3\n";
   stack.push(3);
   std::cout << "Pushing 6\n";
   stack.push(6);

   // Add the two values.
   stack.add();

   // Pop the sum off the stack and display it.
   std::cout << "The sum is ";
   stack.pop(catchVar);
   std::cout << catchVar << std::endl << std::endl;

   // Push 7 and 10 onto the stack
   std::cout << "Pushing 7\n";
   stack.push(7);
   std::cout << "Pushing 10\n";
   stack.push(10);

   // Subtract 7 from 10.
   stack.sub();

   // Pop the difference off the stack and display it.
   std::cout << "\nThe difference is ";
   stack.pop(catchVar);
   std::cout << catchVar << std::endl;

   std::cout << "\n\nPushing 5 then 10";
   stack.push(5);
   stack.push(10);
   std::cout << "\nThe product is ";
   stack.mult();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 7 then 12";
   stack.push(7);
   stack.push(12);
   std::cout << "\nThe product is ";
   stack.mult();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 14 and 2";
   stack.push(14);
   stack.push(2);
   std::cout << "\nThe quotient is ";
   stack.div();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 24 and 12";
   stack.push(24);
   stack.push(12);
   std::cout << "\nThe quotient is ";
   stack.div();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 24, 12, and 3";
   stack.push(24);
   stack.push(12);
   stack.push(3);
   std::cout << "\nThe sum is ";
   stack.addAll();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 44, 47, and 1";
   stack.push(44);
   stack.push(47);
   stack.push(1);
   std::cout << "\nThe sum is ";
   stack.addAll();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 24, 12, and 3";
   stack.push(24);
   stack.push(12);
   stack.push(3);
   std::cout << "\nThe product is ";
   stack.multAll();
   stack.pop(catchVar);
   std::cout << catchVar;

   std::cout << "\n\nPushing 18, 12, and 5";
   stack.push(18);
   stack.push(12);
   stack.push(5);
   std::cout << "\nThe product is ";
   stack.multAll();
   stack.pop(catchVar);
   std::cout << catchVar;

   return 0;
}

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED
#include "NumStack.hpp"
 template <class T> class Stack : public NumStack<T> // added template arg to NumStack
{
public:
   // Constructor
   Stack(T s) : NumStack<T>(s) {}  // added template arg to Numstack

   // MathStack operations
   void add();
   void sub();
   void mult();
   void div();
   void addAll();
   void multAll();
};

#endif // STACK_HPP_INCLUDED  
That fixed all, but one error. In main it still tells me that I'm missing the template arguments before 'stack'.
In main it also says that I'm missing template arguments before 'stack'.

you need to decide whether the derived class is also going to be a template class or not. At the moment the way you've laid out Stack in it's header file seems to suggest that it is a template class while in main() Stack is represented as a non-template class i.e. it is derived from a particular instantiation of NumStack<T>
if you want only NumStack to be template but not Stack it'd be something like:
1
2
template <typename T> class NumStack{};
class Stack : public NumStack<int>{};//Stack derived from NumStack<int> for e.g. 


Also NumStack is missing the template parameter here:
template <class T> class Stack : public NumStack // should be NumStack<T>

As for the other error message: "class 'Stack<T> does not have any field named 'NumStack'"
this is entirely reasonable given that NumStack<T> is expected to be a base for Stack(template or otherwise)
Both classes need to be templates.
template <class T> class Stack : public NumStack<T>
I added NumStack<T>, but it still tells me that I'm missing template arguments.
Stack stack(5);
missing template arguments - Stack is non-template here
Thanks, I'm new with templates and didn't realize that I had to add <int> in main.
In some cases the template arguments will be deduced, so you don't always (for function templates, anyways):
http://en.cppreference.com/w/cpp/language/template_argument_deduction
Last edited on
closed account (Dz1T7k9E)
I'm doing a similar program and am having an error on line 21 of main.
I'm getting this error:
undefined reference to `Stack<int>::add()'


Can someone help me with this error?
Welcome to the forum!

You didn't define or link Stack<>::add(), or you tried to separate the implementation of Stack<>::add from it's declaration.

If you need more help, please open your own thread and post your code, instead of hijacking this one.
Topic archived. No new replies allowed.