dá dando pau e eu n sei pq
Oi, galera.
Tô com um problema aqui: :wacko:
Tenho três arquivos: pilha.h, pilha.cpp e testepilha.cpp
Como é de se esperar, no arquivo pilha.h eu tenho a definição de uma classe, a pilha.
No arquivo pilha.cpp eu tenho as definições das funções da classe pilha e no arquivo testepilha.cpp eu tenho a função main(), onde eu declaro um objeto do tipo pilha e faço algumas operações nele.
O problema é que eu esperava compilar os dois arquivos de uma vez e gerar um executável só, mas o compulador devolve uma mensagem de erro. http://forum.imasters.com.br/public/style_emoticons/default/cry.gif
Se eu incluo todo o arquivo pilha.cpp no testepilha.cpp, o executável e gerado normalmente.
Abaixo seguem os códigos dos arquivos.
Se alguém souber como eu resolvo isso, gostaria que me ajudasse. De preferência me dizendo como eu faço tembém para compilar cada arquivo separadamente e depois linkar tudo (essa parte eu sei, mas com esses arquivos tá meio ruim http://forum.imasters.com.br/public/style_emoticons/default/blush.gif ).
pilha.h
CODE
#ifndef PILHA#define PILHA
#include <iostream>
using namespace std;
template<class tipoPilha = int>
class pilha {
private:
tipoPilha* vet;
tipoPilha *topo, *aux;
tipoPilha temp;
int tam;
short i;
public:
pilha(int);
~pilha(void);
bool vazia(void);
bool insere(int);
tipoPilha remove(void);
void imprime(void);
};
#endif
pilha.cpp
CODE
#include "pilha.h"
template<class tipoPilha>
pilha<tipoPilha>::pilha(int tamanho = 10) {
vet = new tipoPilha[tamanho];
tam = tamanho;
topo = NULL;
}
template<class tipoPilha>
pilha<tipoPilha>::~pilha(void) {
delete []vet;
}
template<class tipoPilha>
bool pilha<tipoPilha>::vazia(void) {
return (topo == NULL);
}
template<class tipoPilha>
bool pilha<tipoPilha>::insere(int valor = 0) {
if(topo != &vet[tam-1]) {
if(vazia()) topo = vet;
else topo++;
*topo = valor;
} else return false;
return true;
}
template<class tipoPilha>
tipoPilha pilha<tipoPilha>::remove(void) {
temp = *topo;
if(topo > vet) topo--;
else topo = NULL;
return temp;
}
template<class tipoPilha>
void pilha<tipoPilha>::imprime(void) {
if(!vazia()) {
for(i=0, aux = vet; aux != topo; i++, aux++) cout << vet* << "\t";*
* cout << vet** << endl;*
} else cout << endl;
}
testepilha.cpp
CODE
*#include "pilha.h"** *
int main() {
pilha<int> p(10);
if(p.vazia()) cout << "Vazia" << endl;
p.insere(5);
p.insere(8);
p.imprime();
p.remove();
p.insere(7);
p.imprime();
p.remove();
if(p.vazia()) cout << "Vazia" << endl;
p.remove();
if(p.vazia()) cout << "Vazia" << endl;
p.imprime();
}
parâmetros de compilação:
g++ pilha.cpp testepilha.cpp -o pilha
mensagem de erro:
>
/tmp/ccNzEElO.o: In function `main':
*testepilha.cpp:(.text+0x93): undefined reference to `pilha<int>::pilha(int)'*
*testepilha.cpp:(.text+0x9e): undefined reference to `pilha<int>::vazia()'*
*testepilha.cpp:(.text+0xd9): undefined reference to `pilha<int>::insere(int)'*
*testepilha.cpp:(.text+0xec): undefined reference to `pilha<int>::insere(int)'*
*testepilha.cpp:(.text+0xf7): undefined reference to `pilha<int>::imprime()'*
*testepilha.cpp:(.text+0x102): undefined reference to `pilha<int>::remove()'*
*testepilha.cpp:(.text+0x115): undefined reference to `pilha<int>::insere(int)'*
*testepilha.cpp:(.text+0x120): undefined reference to `pilha<int>::imprime()'*
*testepilha.cpp:(.text+0x12b): undefined reference to `pilha<int>::remove()'*
*testepilha.cpp:(.text+0x136): undefined reference to `pilha<int>::vazia()'*
*testepilha.cpp:(.text+0x169): undefined reference to `pilha<int>::remove()'*
*testepilha.cpp:(.text+0x174): undefined reference to `pilha<int>::vazia()'*
*testepilha.cpp:(.text+0x1a7): undefined reference to `pilha<int>::imprime()'*
*testepilha.cpp:(.text+0x1b2): undefined reference to `pilha<int>::~pilha()'*
*testepilha.cpp:(.text+0x1cd): undefined reference to `pilha<int>::~pilha()'*
collect2: ld returned 1 exit status
Valeu, pessoal http://forum.imasters.com.br/public/style_emoticons/default/joia.gif
Discussão (4)
Carregando comentários...