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:
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.
– João Paulo