A .hppve .cppdosya arasında bölünmüş bir C ++ şablon sınıfını derlemeye çalışırken hatalar alıyorum :
$ g++ -c -o main.o main.cpp
$ g++ -c -o stack.o stack.cpp
$ g++ -o main main.o stack.o
main.o: In function `main':
main.cpp:(.text+0xe): undefined reference to 'stack<int>::stack()'
main.cpp:(.text+0x1c): undefined reference to 'stack<int>::~stack()'
collect2: ld returned 1 exit status
make: *** [program] Error 1
İşte kodum:
stack.hpp :
#ifndef _STACK_HPP
#define _STACK_HPP
template <typename Type>
class stack {
public:
stack();
~stack();
};
#endif
stack.cpp :
#include <iostream>
#include "stack.hpp"
template <typename Type> stack<Type>::stack() {
std::cerr << "Hello, stack " << this << "!" << std::endl;
}
template <typename Type> stack<Type>::~stack() {
std::cerr << "Goodbye, stack " << this << "." << std::endl;
}
main.cpp :
#include "stack.hpp"
int main() {
stack<int> s;
return 0;
}
ldelbette doğru: semboller yok stack.o.
Zaten söylediği gibi yaptığım için bu sorunun cevabı yardımcı olmuyor.
Bu yardımcı olabilir, ancak her yöntemi tek tek .hppdosyaya taşımak istemiyorum — Yapmamalıyım , değil mi?
.cppDosyadaki her şeyi dosyaya taşımak .hppve bağımsız bir nesne dosyası olarak bağlantı kurmak yerine her şeyi dahil etmek için tek makul çözüm bu mu? Bu çok çirkin görünüyor ! Bu durumda, benim de önceki durumuna ve yeniden adlandırma geri olabilir stack.cppiçin stack.hppve onunla yapılabilir.