Helo I´m having trouble finding out how to use templates properly. Te problem I´m having is, that I can´t find the right way to make the templates work outside the file where the class using the template is declared. Look at the following example.
Template.h file
[PHP]
#ifndef TEMPLATE_H
#define TEMPLATE_H
template<typena me T>
class Template{
private:
T info;
public:
Template(T info);
T getInfo()const;
void setInfo(T info);
};
#endif
[/PHP]
Template.cpp file
[PHP]
#include "Template.h "
template<typena me T>
Template<T>::Te mplate(T info){
this->info = info;
}
template<typena me T>
T Template<T>::ge tInfo()const{
return this->info;
}
template<typena me T>
void Template<T>::se tInfo(T info){
this->info = info;
}
[/PHP]
[PHP]
#include <iostream>
#include "Template.h "
using namespace std;
int main(){
Template< int > t(1);
cout<<t.getInfo ()<<endl;
}
[/PHP]
the error message the compiler tells me is the following:
[HTML]
Error: Unresolved external 'Template<int>: :Template<int>( int)' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ
Error: Unresolved external 'Template<int>: :getInfo() const' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ
[/HTML]
Any help will be greatly apreciated!
Michael
Template.h file
[PHP]
#ifndef TEMPLATE_H
#define TEMPLATE_H
template<typena me T>
class Template{
private:
T info;
public:
Template(T info);
T getInfo()const;
void setInfo(T info);
};
#endif
[/PHP]
Template.cpp file
[PHP]
#include "Template.h "
template<typena me T>
Template<T>::Te mplate(T info){
this->info = info;
}
template<typena me T>
T Template<T>::ge tInfo()const{
return this->info;
}
template<typena me T>
void Template<T>::se tInfo(T info){
this->info = info;
}
[/PHP]
[PHP]
#include <iostream>
#include "Template.h "
using namespace std;
int main(){
Template< int > t(1);
cout<<t.getInfo ()<<endl;
}
[/PHP]
the error message the compiler tells me is the following:
[HTML]
Error: Unresolved external 'Template<int>: :Template<int>( int)' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ
Error: Unresolved external 'Template<int>: :getInfo() const' referenced from C:\DOCUMENTS AND SETTINGS\MICHAE L SCHMIDT\CBPROJE CT\TEMPLATE\WIN DOWS\DEBUG_BUIL D\MAIN.OBJ
[/HTML]
Any help will be greatly apreciated!
Michael
Comment