Error with Template

0

Please, could you help me!?

Errors are for all headers in the stack.cpp file that references stack.h. It involves use of Templates.

Battery code.h:

#ifndef PILHA_H
#define PILHA_H

#include <iostream>
//#include <cstdlib>

template <typename T>
class Pilha{

    T * vetor;
    int tam;
    int topo;

public:
    Pilha(int size);
    ~Pilha();

    void Print();
    void Push(T vetor);
    T Pop();
    T Topo();
    void Limpar();

    bool Vazio();
    bool Cheio();

};

#include "pilha.cpp"

#endif

Battery code.cpp:

#include<iostream>
#include "pilha.h"

//Construtor
template <typename T>
Pilha<T>::Pilha(int size){
    vetor = new T[size];
    this->tam = size;
    topo = -1;
}
//Destrutor: remove o vetor completamente
template <typename T>
Pilha<T>:: ~Pilha(){
    delete [] vetor;
    topo = -1;
}
//Inserindo conteúdo:
template <typename T>
void Pilha<T>::Push(T n){
    if(Cheio())
        std:: cout << "Erro: Pilha cheia" << std::endl;
    else
        this->vetor[++topo] = n;
    //topo++;
}
//Removendo conteúdo;
template <typename T>
T Pilha<T>::Pop(){
    T aux = Topo();
    topo--;
    return aux;
}
//Topo da pilha:
template <typename T>
T Pilha<T>::Topo(){
    if(Vazio())
        std::cout << "Erro: Pilha cheia" << std::endl;
    else
        return vetor[topo];
}
//Limpando a pilha:
template <typename T>
void Pilha<T>::Limpar(){
    delete [] vetor;
    topo = -1;  
}
//Pilha vazia:
template <typename T>
bool Pilha<T>::Vazio(){
    if(topo == -1){
        std::cout << "Pilha vazia" << std::endl;
        return true;
    }
    else
        return false;
}
//Pilha cheia:
template <typename T>
bool Pilha<T>::Cheio(){
    if(topo == tam - 1){
        std::cout << "Pilha Cheia" << std::endl;
        return true;
    }
    else
        return false;
}
//Exibindo os elementos da pilha:
template <typename T>
void Pilha<T>::Print(){
    for(int i = topo; i > -1; i--){
        std::cout << vetor[i] << " ";
    }
}

Build Screen:

    
asked by anonymous 22.05.2018 / 16:46

1 answer

1

The definition of entities in a template must be in the same translation unit where they are used. Separating declaration in headers and setting in translation units does not work with templates.

The reason for this is that the compiler needs to know what the template definition is when it is instantiated. If only the statement is visible at the instance point, the compiler will complain (as seen in your compiler's error messages.) And no, there is no way for the compiler to know the declaration definition of a template that is separate on another translation, since there is no information linking to one another (in contrast to normal code, it is not necessary to know the definition of a function to use it.)

With all that said, the solution is to put the definitions of your member functions inside the header and remove the translation unit.

And, finally, you're including "pilha.cpp" in your header by duplicating the settings.

    
22.05.2018 / 17:06