C ++ template class definition and Declaration

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.