0

i am not able to create object of LO class. the code is below.

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

in the main function i am creating LO class object and throwing, LO class is inheriting Tem class which is template, inside Tem class i have a pointer of UDPTest class.

As i know, i am doing mistake somewhere but i am not able to know. please help me

Thanks

1 Answers1

1

When using class templates in C++ you have to explicitly define the members of those classes with the declaration, in the header file, otherwise you won't be able to compile it.

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

Community
  • 1
  • 1
Mmmh mmh
  • 5,334
  • 3
  • 21
  • 29