I am working on a class assignment, relatively new to the c++ language and I have tried to function the code to hopefully get rid of the error but, have not been successful. can anyone help me out?, I am primarily working with mathstack template class and calling it to push a number to the stack but the following code comes up.
error message : clang++-7 -pthread -std=c++17 -o main MathStack.cpp main.cpp
/tmp/main-69d6a9.o: in function 'menu(int&)'; main.cpp:(.text+0x297): undefined reference to
'MathStack<int>::push(int)'clang: error : linker command failed with code 1 (use -v to see
invocation)
**/ Implementation file for the MathStack class**
#include "MathStack.h"
#include <iostream>
//***********************************************
// Member function add. add pops *
// the first two values off the stack and *
// adds them. The sum is pushed onto the stack. *
//***********************************************
template <class T>
void MathStack<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);
}
//***********************************************
// Member function sub. sub pops the *
// first two values off the stack. The *
// second value is subtracted from the *
// first value. The difference is pushed *
// onto the stack. *
//***********************************************
template <class T>
void MathStack<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);
}
//**********************************************
// Member function mult. multiplies first two *
// values of the stack. *
//**********************************************
template <class T>
void MathStack<T>::mult()
{
T num, res;
// pop the first two values of the stack
pop(res);
pop(num);
// multiply thw two values
res = res*num;
// push result back into the stack
push(res);
}
template <class T>
//*********************************************
// Member funciton div. divides the first two *
// values of the stack. *
// shows an error when divided by zero *
//*********************************************
void MathStack<T>::div()
{
T quot, divisor;
// pop the first two values of the stack
pop(divisor);
pop(quot);
// exexption
if (divisor == 0)
{
cout << "ERROR: Cannot divide by zero.\n";
}
else {quot = quot/divisor; } // division formula
// push quotient back into the stack
push(quot);
}
//*************************************************
// Member function push pushes the argument onto *
// the stack. *
//*************************************************
template <class T>
void MathStack<T>::push(T num)
{
if (isFull())
{
cout << "The stack is full.\n";
}
else
{
top++;
stackArray[top] = num;
}
}
//****************************************************
// Member function pop pops the value at the top *
// of the stack off, and copies it into the variable *
// passed as an argument. *
//****************************************************
template <class T>
void MathStack<T>::pop(T &num)
{
if (isEmpty())
{
cout << "The stack is empty.\n";
}
else
{
num = stackArray[top];
top--;
}
}
//***************************************************
// Member function isFull returns true if the stack *
// is full, or false otherwise. *
//***************************************************
template <class T>
bool MathStack<T>::isFull() const
{
bool status;
if (top == stackSize - 1)
status = true;
else
status = false;
return status;
}
//****************************************************
// Member funciton isEmpty returns true if the stack *
// is empty, or false otherwise. *
//****************************************************
template <class T>
bool MathStack<T>::isEmpty() const
{
bool status;
if (top == -1)
status = true;
else
status = false;
return status;
}
// file specifies the stack class and use of math functions
#include <iostream>
#include "MathStack.h"
using namespace std;
// Constants for the menu choices
const int Add = 1,
Subtract = 2,
Multiply = 3,
Division = 4,
QUIT = 5;
// Function prototypes
void menu(int &);
void getStackSize(int &);
int main()
{
int stackSize; // The stack size
int choice; // To hold a menu choice
menu(choice);
// Create the stack.
MathStack<int> Math(stackSize);
MathStack<float> Math1(stackSize);
return 0;
}
//************************************************
// The menu function displays the menu and gets *
// the user's choice, which is assigned to the *
// reference parameter. *
//************************************************
void menu(int &choice)
{
int stackSize;
int number;
getStackSize(stackSize);
// Display the menu and get the user's choice.
cout << "\nWhat do you want to do?\n"
<< Add
<< " - Add items from the stack\n"
<< Subtract
<< " - Subtact items from the stack\n"
<< Multiply
<< " - Multiply items from the stack\n"
<< Division
<< " - Divide items form the stack\n"
<< QUIT
<< " - Quit the program\n"
<< "Enter your choice: ";
cin >> choice;
// Validate the choice
while (choice < Add || choice > QUIT)
{
cout << "Enter a valid choice: ";
cin >> choice;
}
switch(choice)
{
case 1:
MathStack<int> Math(stackSize);
for (int i = 0; i < stackSize; i++)
{
cout << " Enter number to insert to the stack:\n" << endl;
}
cin >> number;
Math.push(number);
}
}
//************************************************
// The getStackSize function gets the desired *
// stack size, which is assigned to the *
// reference parameter. *
//************************************************
void getStackSize(int &size)
{
// Get the desired stack size.
cout << "How big should I make the stack? ";
cin >> size;
// Validate the size.
while (size < 1)
{
cout << "Enter 1 or greater: ";
cin >> size;
}
}