我无法创建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;
}
在main函数中我正在创建LO类对象并抛出,LO类继承了Tem类,它是模板,在Tem类中我有一个UDPTest类的指针。
据我所知,我在某处犯了错误,但我无法知道。请帮帮我
由于
答案 0 :(得分:1)
在C ++中使用类模板时,必须在头文件中使用声明显式定义这些类的成员,否则您将无法编译它。
(见Why can templates only be implemented in the header file?)