Declaration and use of class templates: if a member function is defined outside the class template, it should be written as a class template:
Template <class virtual type parameter>
Function Type Template Name <virtual type parameter> member function name (function form parameter table ){...}
The test template class is as follows:
// Compare. h
# Ifndef _ compare_h
# DEFINE _ compare_h
Template <typename T>
Class compare
{
Public:
~ Compare (void );
Compare (t, t );
T max ();
T min ();
PRIVATE:
T x;
T y;
};
# Endif
// compare. CPP
# include "compare. H "
template
compare : Compare (t a, t B)
{< br> X =;
Y = B;
}< br> template
compare ::~ Compare (void)
{< BR >}< br> template
t compare :: max ()
{< br> return (x> Y )? X: Y;
}< br> template
t compare : min ()
{< br> return (x }< br> the above two files are compiled successfully in vs2008, then an error occurs when the following main function is added.
# include
using namespace STD;
# include "compare. H "
int main ()
{< br> compare TT (1, 2);
cout system ("pause");
getchar ();
return 0;
}< br> I still cannot find any reason after a long time. Error message: public: _ thiscall compare ::~ Compare (void )"(?? 1?
$ Compare @ H @ Qae @ xz ).
Later, I found out that the current compiler does not support the class template definition and implementation.
Therefore, the content of compare. cpp is cut to compare. H, and it is normal. There is also a method on the Internet, which is usedExportBefore adding the template, this method still fails in vs2008. Prompt: This keyword export is retained and may be used later...
Conclusion: For most compilers at present, the template definition and template declaration must be put together before compilation is successful.