Error with Template

Asked

Viewed 115 times

0

Please, could you help me!?

The errors are for all the headers of the.cpp stack file that refers to.h. Involves using 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

Code of the batteries.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] << " ";
    }
}

Compilation screen:

inserir a descrição da imagem aqui

  • The problem was that the include of a source file (cpp) was made, which resulted in the redefinition of class functions. You cannot include a file . cpp, only headers that is correct.

1 answer

1


The entity definition of a template must be on the same translation unit where it is used. Separation of declaration into headers and definition into translation units does not work with templates.

The reason for this is that the compiler needs to know how 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 the compiler knows the definition of the declaration of a template that is separated into another translation unit, as there is no information linking one to another (in contrast to normal code, which is not necessary to know the definition of a function to use it.)

With all this said, the solution is to put the settings of your member functions within the header and remove the translation unit.

And finally, you’re including "pilha.cpp" in its header, duplicating the definitions.

  • It successfully compiled right after deleting the line --> #include "stack.cpp" at the end of the stack.h file. It was the only change I made. Thanks!

  • @LUIZ, but do not forget to make the other change. As soon as you include "pilha.h" in another unit, without the definition, the code will not compile.

  • You only successfully compiled and executed Main.cpp after I rewrote the line including.cpp stack, after I generated the file ". o"

  • Completed successfully! Removing the #include line "stack. h" at the beginning of the file "stack.cpp". I managed to keep the 3 files.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.