Template class won’t allow usage of T type variables

I have a class template Stack which needs a T type variable data. However, Visual studio gives me the error C2079 'Stack<T>::data' uses undefined class 'T' However, it has no problem using T type on peek(); What am I supposed to do to fix it? I will attach my class and implementation below.

#pragma once
#include<cstdlib>
#include<iostream>


template <class T>
 class Stack
{
public:
    Stack();
    ~Stack();
    T data; //'Stack<T>::data uses undefined class 'T' 
    Stack* link;
    Stack* top;

    void push(T);
    bool isEmpty();
    T peek();
    void pop();
    void display();
};
#include "Stack.h"

template <class T>
Stack<T>::Stack()
{

}

template <class T>
Stack<T>::~Stack()
{
}

template <class T>
void Stack<T>::push(T data) {
    Stack* temp;
    temp = new Stack();

    if (!temp) {
        std::cout << "Stack Overflow";
        std::exit(true);
    }

    temp->data = data;

    temp->link = top;

    top = temp;
}

template <class T>
bool Stack<T>::isEmpty() {
    return top == NULL;
}

template <class T>
T Stack<T>::peek() {
    if (!isEmpty()) {
        return top->data;
    }
    else
        exit(true);
}

template <class T>
void Stack<T>::pop() {
    Stack* temp;

    if (top == NULL) {
        std::cout << "Stack Underflow" << std::endl;
        exit(true);
    }

    else {
        temp = top;

        top = top->link;

        temp->link = NULL;

        delete temp;
    }
}

template <class T>
void Stack<T>::display() {
    Stack* temp;

    if (top == NULL) {
        std::cout << "Stack Underflow";
        exit(true);
    }
    else {
        temp = top;
        while (temp != NULL) {
            std::cout << temp->data << " ";

            temp = temp->link;
        }
    }
}


Read more here: https://stackoverflow.com/questions/66522868/template-class-wont-allow-usage-of-t-type-variables

Content Attribution

This content was originally published by killerkody gaming at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: