Undefined Reference Error

closed account (Dz1T7k9E)
I keep getting this error
undefined reference to 'Stack<int>::add()'


How am I supposed to fix this?

For reference here are my associated files

Main.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
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
112
#include <iostream>

#include "NumStack.hpp"
#include "stack.hpp"


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

   // Create a MathStack object.
   Stack <int> 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 << "The difference is ";
   stack.pop(catchVar);
   std::cout << catchVar << std::endl << std::endl;;

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

   // Multiply the two numbers
   //stack.mult();

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

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

   // Divide the two numbers
   //stack.div();

   //Pop the product off the stack and display it.
   std::cout << "The quotient is ";
   stack.pop(catchVar);
   std::cout << catchVar << "\n\n";

   //Pushing 5 numbers: 1, 2, 3, 4 & 5 to the stack
   std::cout << "Pushing 1\n";
   stack.push(1);
   std::cout << "Pushing 2\n";
   stack.push(2);
   std::cout << "Pushing 3\n";
   stack.push(3);
   std::cout << "Pushing 4\n";
   stack.push(4);
   std::cout << "Pushing 5\n";
   stack.push(5);

   // Add all the numbers
   //stack.addAll();

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

   //Pushing 5 numbers: 2, 4, 3, 8 & 10 to the stack
   std::cout << "Pushing 2\n";
   stack.push(2);
   std::cout << "Pushing 4\n";
   stack.push(4);
   std::cout << "Pushing 3\n";
   stack.push(3);
   std::cout << "Pushing 8\n";
   stack.push(8);
   std::cout << "Pushing 10\n";
   stack.push(10);

   // Multiply all the numbers
   //stack.multAll();

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

   return 0;
}


Stack.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
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
#include "NumStack.hpp"
#include "Stack.hpp"

template <class T>
void Stack<T>::add()
{
   T num, sum;

   // Pop the first two values off the stack.
   pop(sum);
   pop(num);

   // Add the two values, store in sum.
   sum += num;

   // Push sum back onto the stack.
   push(sum);
}

template <class T>
void Stack<T>::sub()
{
   T num, diff;

   // Pop the first two values off the stack.
   pop(diff);
   pop(num);

   // Subtract num from diff.
   diff -= num;

   // Push diff back onto the stack.
   push(diff);
}


template <class T>
void Stack<T>::mult()
{
    T num, product;

    //Pop the first two values off the stack
    pop(product);
    pop(num);

    //Multiply num and product
    product *= num;

    //Push product back onto the stack
    push(product);
}


template <class T>
void Stack<T>::div()
{
    T num, quotient;

    //Pop the first two values off the stack
    pop(num);
    pop(quotient);

    //Divide num from quotient
    quotient /= num;

    //Push product back onto the stack
    push(quotient);
}

template <class T>
void Stack<T>::addAll()
{
   T num(0), sum(0);

   //While loop to pop all members off the stack
   while (!this.isEmpty())
   {
       pop(num);
       sum += num;
   }

   push(sum);
}

template <class T>
void Stack<T>::multAll()
{
    T num(0), product(1);

    //While loop to pop all members off the stack
    while(!this.isEmpty())
    {
        pop(num);
        product *= num;
    }

    push(product);
}


Stack.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
#ifndef STACK_HPP_INCLUDED
#define STACK_HPP_INCLUDED

#include "NumStack.hpp"

template <class T> class Stack : public NumStack<T>
{
public:
   // Constructor
   Stack(T s) : NumStack<T>(s) {}

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



#endif // STACK_HPP_INCLUDED


Help would be greatly appreciated!
Last edited on
closed account (Dz1T7k9E)
Thanks!
Topic archived. No new replies allowed.