lundi 27 avril 2020

How to Define a template Class in a .h File and Implement it in a .cpp File

I was working on explicit class template instantiation and my goal is to separate .h functions declarations inside the class and implementation of those functions inside .cpp file.

I have a sample program here:

file: stack_example.h 

#pragma once
#include <iostream>
#include <random>

template<typename T>
class stack_example
{
    T m_Buffer[10];
    int m_Top;
public:
    stack_example() {
        m_Buffer[10] = { 0 };
        m_Top = -1;
    }
//  ~stack_example();
    void push(const T& elem);
    void pop();
    const T& Top();
    bool IsEmpty();
    void display();

};
file: stack_example.cpp 

#include "stack_example.h"

template<typename T>
void stack_example<T>::push(const T& elem){
    m_Buffer[++m_Top] = elem;
}

template<typename T>
const T& stack_example<T>::Top() {
    return m_Buffer[m_Top];
}

template<typename T>
bool stack_example<T>::IsEmpty(){
    return m_Top == -1;
}

template<typename T>
void stack_example<T>::pop() {
    std::cout << "Deleted element is: " << m_Buffer[m_Top] << std::endl;
    m_Top--;
}

template<typename T>
void stack_example<T>::display() {
    for (int i = 0; i <= m_Top;i++) {
        std::cout << m_Buffer[i] << std::endl;
    }
}

#include "stack_example.h"

template<typename T>
void stack_example<T>::push(const T& elem){
    m_Buffer[++m_Top] = elem;
}

template<typename T>
const T& stack_example<T>::Top() {
    return m_Buffer[m_Top];
}

template<typename T>
bool stack_example<T>::IsEmpty(){
    return m_Top == -1;
}

template<typename T>
void stack_example<T>::pop() {
    std::cout << "Deleted element is: " << m_Buffer[m_Top] << std::endl;
    m_Top--;
}

template<typename T>
void stack_example<T>::display() {
    for (int i = 0; i <= m_Top;i++) {
        std::cout << m_Buffer[i] << std::endl;
    }
}

#include "stack_example.h"

template<typename T>
void stack_example<T>::push(const T& elem){
    m_Buffer[++m_Top] = elem;
}

template<typename T>
const T& stack_example<T>::Top() {
    return m_Buffer[m_Top];
}

template<typename T>
bool stack_example<T>::IsEmpty(){
    return m_Top == -1;
}

template<typename T>
void stack_example<T>::pop() {
    std::cout << "Deleted element is: " << m_Buffer[m_Top] << std::endl;
    m_Top--;
}

template<typename T>
void stack_example<T>::display() {
    for (int i = 0; i <= m_Top;i++) {
        std::cout << m_Buffer[i] << std::endl;
    }
}
file: main.cpp

#include "stack_example.h"
#include "stack_example.cpp"

int main() {

    std::random_device dev;
    std::default_random_engine engine(dev());
    std::uniform_int_distribution<>dist(10,20);
    stack_example<int> s;
    for (int i = 0; i < 5;i++) {
        s.push(dist(engine));
    }

    s.display();

    return 0;
}

here in main.cpp if i include stack_example.cpp then it compiles and executes but if its not included then i get error like this

Error   LNK2019 unresolved external symbol "public: void __thiscall stack_example<int>::push(int const &)" (?push@?$stack_example@H@@QAEXABH@Z) referenced in function _main    class_template      

I know this is related to class template instantiation, but i dont have a clear understanding and i read in some resources https://www.codeproject.com/Articles/48575/How-to-Define-a-Template-Class-in-a-h-File-and-Imp regarding this, Only i got is work around but not about class template instantiation. PLease can anyone let me know how to get a solution to this?

Aucun commentaire:

Enregistrer un commentaire