SORU
12 Kasım 2009, PERŞEMBE


Bölme sınıflara C şablon oluşturdu .dosyaları mümkün hpp/.cpp ?

Hatalar .hpp .cpp dosya arasında bölünmüş C sınıf bir şablon derlemeye çalışıyorum 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 benim kod:

yığın.hes:

#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;
}

ld elbette doğrudur: simgeler stack.o. değil

this question cevabı zaten yazıyor gibi yapıyorum yardım etmez.
This one yardımcı olabilir, ama .hpp dosyası içine her yöntem hareket-etmek istemiyorum, gerek yok, ben mi söyleyeyim?

Tek mantıklı çözüm .hpp dosya .cpp dosyasındaki her şeyi taşımak, ve sadece her şeyi dahil etmek yerine, bağımsız bir nesne dosyası olarak link? Bu gibi görünüyorçokçirkin! Bu durumda, benim önceki durumuna döner ve stack.hpp stack.cpp yeniden adlandırın ve onunla yapılabilir.

CEVAP
12 Kasım 2009, PERŞEMBE


mümkün ihtiyacımız olacak ne biliyorsun sürece.

Stack.cpp sonunda aşağıdaki kodu ekleyin ve bu iş olacak :

template class stack<int>;

Yığın olmayan şablon tüm yöntemler oluşturulacak ve adım bağlama iyi çalışır.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Gavin Hoey

    Gavin Hoey

    21 Aralık 2007
  • NLthomas21

    NLthomas21

    20 Mayıs 2008
  • The Warp Zone

    The Warp Zon

    24 AĞUSTOS 2007