0

This is the stack implementation file StackType.cpp

This include header files stacktype.h and student.h

#include"StackType.h"
#include"Student.h"
using namespace std;
template <class elemType>
StackType<elemType>::StackType(int size)
{
    array = new elemType[size];
    maxStackSize = size;
    stackTop = -1;
}
template <class elemType>
bool const StackType<elemType>::isEmpty() {
    if (stackTop == -1)
    {
        return true;
    }
    else
        return false;
}

template <class elemType>
bool const StackType<elemType>::isFull() {
    if (stackTop+1 == maxStackSize)
        return true;
    else
        return false;
}

template <class elemType>
void StackType<elemType>::push(elemType e)
{
    if (isFull())
    {
        cout << "The stack is full\n";
    }
    else
    {
        array[++stackTop] = e;
    }
    
}

template <class elemType>
void StackType<elemType>::pop()
{
    if (isEmpty())
    {
        cout << "The Stack is Empty\n";
    }
    else
    {
        array[stackTop--];
    }
    
}
template <class elemType>
elemType const StackType<elemType>::top()
{
    if (!isEmpty()) {
        return array[stackTop];
    }
    else {
        cout << "The stack is Empty";
    }
}

template <class elemType>
int const StackType<elemType>::getStackTop() {
    return stackTop;
}

template <class elemType>
elemType const StackType<elemType>::getElement(int position) {
    if (position < maxStackSize && position >= 0)
    {
        if (!isEmpty())
        {
            return array[position];
        }
        else
            cout << "Position is not valid" << endl;
    }
}

StackType.h

#pragma once
#ifndef StackType_H
#define StackType_H
#include<string>
#include<iostream>
template <class elemType>
class StackType {
public:
    StackType();
    int maxStackSize;
    int stackTop;
    elemType* array;

    StackType(int size);
    bool const isEmpty();
    bool const isFull();
    int const getStackTop();
    void push(elemType e);
    void pop();
    elemType const top();
    elemType const getElement(int position);
    ~StackType() {
        delete[] array;
    }
};
#endif

main.cpp

This is the main file that contains other header files main.CPP(main file)

#include<iostream>
#include"StackType.h"
using namespace std;
int main()
{
    cout << "Stack app!\n--------------------\n";
    StackType<int> in(5);
    cout << "Elements in the integer stack:\n";
    in.push(2);
    in.push(4);
    in.push(3);
    in.push(6);
    in.push(7);
    for (int i = 0; i <= in.getStackTop(); i++)
    {
        cout << in.getElement(i) << " ";
    }
    cout << "\nAfter popping one eelement, the updated integeer stack is:\n";
    in.pop();
    for (int i = 0; i <= in.getStackTop(); i++)
    {
        cout << in.getElement(i) << " ";
    }
}

Error

Blockquote

  1. This is the Error I am getting while compiling I have tried on sublime text as well as on the terminal I have tried removing the scope resolution operator also compiling all the files separately but still it is showing undefined symbols

     g++ Jaheda_Lima_1503.cpp StackType.cpp Student.cpp -o main
     Undefined symbols for architecture x86_64:
     "StackType<int>::getElement(int)", referenced from:
       _main in Jaheda_Lima_1503-a8a24c.o
     "StackType<int>::getStackTop()", referenced from:
       _main in Jaheda_Lima_1503-a8a24c.o
     "StackType<int>::pop()", referenced from:
       _main in Jaheda_Lima_1503-a8a24c.o
     "StackType<int>::push(int)", referenced from:
       _main in Jaheda_Lima_1503-a8a24c.o
     "StackType<int>::StackType(int)", referenced from:
       _main in Jaheda_Lima_1503-a8a24c.o
     ld: symbol(s) not found for architecture x86_64
     clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

0 Answers0