I write a simple program
templatex.h
templatex.cppCode:#ifndef TEMPLATEXH #define TEMPLATEXH class Templatex { public: void print(); template <typename T> T const& getmax(T const &x , T const &y); }; #endif
templateuse.cppCode:#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; };
Here in main why can not we call t->getmax();Code:#include <iostream> #include "templatex.h" using namespace std; int main() { Templatex* t=new Templatex(); t->print(); }
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.



LinkBack URL
About LinkBacks


