Compilation of C ++ template classes

Source: Internet
Author: User

<Transferred from "software craftsman notes": Workshop>

 

Generally, the C ++ class is published using the. h file write class definition and the CPP file write implementation. At the time of release, the. O files compiled by the hfile and CPP can be sent to the client program, and then the client program can compile and link Class header files and target files. Although CPP source files can also be published, the release of. O files can better maintain the relative independence of classes, or the source code implementation is inconvenient for others to see. For example, a Class header file: // Rec. h # ifndef _ rec_h _
# DEFINE _ rec_h _
 
Class rec
{

Public:
Rec (int );
Void show ();
PRIVATE:
Int M_a;
};
 
 
# Endif class implementation file. // Rec. cpp # include "Rec. H"
# Include <iostream>
Using namespace STD;

REC: REC (int)
{
M_a =;
}

Void REC: Show (){
Printf ("item = % d/N", M_a );
} When using the class, G ++-C rec. cpp generates Rec. O backup. Write a test program // test. cpp # include "Rec. H"

Void main ()
{
Rec a (10 );
A. Show ();
} Compile the test program. Run g ++-O test. cpp Rec. O: item = 10. If the rec class is changed to a template class, the compilation method cannot be so elegant. Rect header file // rect. h
# Ifndef _ rect_h _
# DEFINE _ rect_h _

Template <class T>
Class rect {
Public:
Rect (t );
Void show ();
PRIVATE:
T M_a;
};

# Endif //! # Ifndef _ rect_h _ rect class implementation file: # rect. cpp # include "rect. H"
# Include <iostream>
Using namespace STD;

Template <class T>
Rect <t>: rect (T)
{
M_a = T;
}

Template <class T>
Void rect <t >:: show (){
Cout <"item =" <M_a <Endl;
} Test program: Use a template class, t replace # include "rect. H" with int"

Void main ()
{
Rect <int> A (10 );
A. Show ();
} In vs6, a compilation error occurs: compiling...
Test. cpp
Linking...
Test. OBJ: Error lnk2001: unresolved external symbol "public: void _ thiscall rect <int >:: show (void )"(? Show @? $ Rect @ H @ qaexxz)
Test. OBJ: Error lnk2001: unresolved external symbol "public: _ thiscall rect <int >:: rect <int> (INT )"(?? 0? $ Rect @ H @ Qae @ H @ Z)
Debug/templatetest.exe: Fatal error lnk1120: 2 unresolved externals
Error executing link.exe. Error Analysis: Test. cpp is successful in compiling the target file and an error occurs during the link. The rect <int> class method cannot be found. In fact, rect. cpp cannot generate the target file independently. The reason is simple: because the T type is unknown before use, and the memory allocation cannot be determined, the target file cannot be generated. Therefore, the template class cannot be compiled in two steps. The implementation file of the template class must be compiled with the program using the class. The compiler can compile the corresponding class only after knowing the type required by the program. Modify the Test Program # include "rect. cpp"

Void main ()
{
Rect <int> A (10 );
A. Show ();
} Compiled and successfully run original address: http://linhs.blog.51cto.com/370259/127482

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.