I write a simple program
templatex.h
Code:
#ifndef TEMPLATEXH
#define TEMPLATEXH
class Templatex
{
public:
void print();
template <typename T>
T const& getmax(T const &x , T const &y);
};
#endif
templatex.cpp
Code:
#include <iostream>
#include "templatex.h"
using namespace std;
void Templatex::print()
{
cout << "print";
}
template <typename T>
T const& Templatex::getmax(const T &x, const T &y)
{
if ( x>y )
return x;
else
return y;
};
templateuse.cpp
Code:
#include <iostream>
#include "templatex.h"
using namespace std;
int main()
{
Templatex* t=new Templatex();
t->print();
}
Here in main why can not we call t->getmax();
Why can not compiler see the definition befaore main?
they said that:
"When a function template is used in a way that triggers its instantiation, a compiler will (at some point) need to see that template's definition."
Can someone clarify this subject please .I don't understand.
I am looking for your answers.
Thanks.