2014-01-23 3 views
0

Я не могу создать объект класса LO. код ниже.Неопределенная ссылка при создании объекта

Tem.h 
    #ifndef TEM_H_ 
    #define TEM_H_ 

    #include <iostream> 
    #include <exception> 
    #include "UDPTest.h" 

    template<class T> 
    class Tem : public std::exception { 
    public: 
    T* UDPt; 
    Tem(T& t) throw(); 
    virtual ~Tem() throw(); 
    }; 

    template<class T> 
    class LO :public Tem<T> { 
    char* mez; 
    public: 
    LO() throw() ; 
    char* what() const throw(); 
    virtual ~LO() throw(); 
    }; 

    template<class T> 
    class AT :public Tem<T>{ 
    char* mez; 
    public: 
    AT() throw(); 
    char* what() const throw(); 
    virtual ~AT() throw(); 
    }; 
    #endif /* TEM_H_ */ 



Tem.cpp 



#include "Tem.h" 
#include <iostream> 
using namespace std; 


template<class T> 
Tem<T>::Tem(T& t) throw() : exception(){ 
UDPt = t; 
// TODO Auto-generated constructor stub 
} 
template<class T> 
Tem<T>::~Tem() throw() { 
// TODO Auto-generated destructor stub 
} 

template<class T> 
LO<T>::LO() throw() { 
cout<<"LO constructor"<<endl; 
} 
template<class T> 
LO<T>::~LO() throw(){ 
} 
template<class T> 
char* LO<T>::what() const throw(){ 
return "Lockout validation failed."; 
} 

template<class T> 
AT<T>::AT() throw(){ 
cout<<"AT validation failed."<<endl; 
} 
template<class T> 
char* AT<T>::what() const throw(){ 
return "Active Task validation failed."; 
} 
template<class T> 
AT<T>::~AT() throw(){ 
} 





UDPTest.h 


#ifndef UDPTEST_H_ 
#define UDPTEST_H_ 

class UDPTest { 
public: 
int udp; 
int tcp; 
UDPTest(); 
virtual ~UDPTest(); 

}; 

#endif /* UDPTEST_H_ */ 




templatetest.cpp 

#include <iostream> 
#include "Tem.h" 
#include "UDPTest.h" 
//#include <exception> 
using namespace std; 

int main() { 
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! 
try{ 
    throw LO<int>(); 
    //LO<int> loo; 
}catch(exception& e){ 
    cout<<"got"; 
} 
return 0; 
} 

в основной функции я создаю ЛО объект класса и метание, LO класс наследует класс Tem, который является шаблон, внутри класса Tem я есть указатель класса UDPTest.

Как я знаю, я делаю ошибку где-то, но я не знаю. пожалуйста, помогите мне

Благодаря

+0

есть неопределенная ссылка ошибки, где я бросать объект. Я не знаю, что происходит. –

ответ

1

При использовании шаблонов классов в C++ вы должны явно определить членов этих классов с декларацией, в заголовочном файле, в противном случае вы не сможете скомпилировать его.

(см Why can templates only be implemented in the header file?)

+0

спасибо ... его сделали. –

Смежные вопросы