A template class is written, which separates the definition from the declaration like other classes. It is found that the function definition cannot be found only by referencing the. h file. View the relevant information and find the following explanation:
- Whether the template is a class or function. A template is a "pattern" used by the compiler to generate similar classes or functions.
- For the code generated by the compiler, it must also see the template definition (not just Declaration) and specific type/any type used for the "fill in" template. For example, if you want to use a foo <int>, the compiler must see both the foo template and the specific Foo <int> you want to call.
- The compiler may not remember the details of another. cpp file when compiling other. cpp files. It works, but most do not. If you are reading this FAQ, it is almost certainly not. By the way, this is the so-called "independent compilation model ".
Now, based on these facts, the following is an example of why it looks like this. Suppose you have a template such as Foo declaration:
Template <typename T>
Class Foo {
Public:
Foo ();
Void somemethod (t x );
PRIVATE:
T x;
};
Similarly, the template member function is defined as follows:
Template <typename T>
Foo <t>: Foo ()
{
...
}
Template <typename T>
Void Foo <t >:: somemethod (t x)
{
...
}
Now, assume that some code in the file bar. cpp uses Foo <int>:
// Bar. cpp
Void blah_blah_blah ()
{
...
Foo <int> F;
F. somemethod (5 );
...
}
Obviously, someone somewhere will have to call the constructor of the "pattern", somemethod () function, and instantiate T as Int. However, if you put the constructor and somemethod () definitions in the file Foo. CPP, when compiling Foo. CPP, the compiler will see the template code; when compiling bar. the compiler will see Foo <int>. However, the template code and foo <int> are never displayed at any time. Therefore, the above rule no. 2 does not generate the foo <int>: somemethod () code.
Solution:
1. Write the Declaration and definition in a file
2. Include. h and CPP
For example, consider that the foo. h header file contains the following template function declaration:
// File "foo. H"
Template <typename T>
Extern void Foo ();
Now let's assume that the file Foo. cpp actually defines the template function:
// File "foo. cpp"
# Include <iostream>
# Include "foo. H"
Template <typename T>
Void Foo ()
{
STD: cout <"here I am! \ N ";
}
Assume that the main. cpp file uses this template function to call Foo <int> ():
// File "Main. cpp"
# Include "foo. H"
Int main ()
{
Foo <int> ();
...
}
If you compile and (attempt) link these two. cpp files, most compilers generate a link error. There are three solutions. The first solution is physically defined in the. h file, even if it is not an inline function. This solution may (or may not !) This causes major code expansion, which means that the size of the executable file may increase significantly (or, if your compiler is smart enough, it may not ).
Another solution is to keep the definition in the. cpp file and add only the row template void Foo <int> () to the. cpp file:
// File "foo. cpp"
# Include <iostream>
# Include "foo. H"
Template <typename T> void Foo ()
{
STD: cout <"here I am! \ N ";
}
Template void Foo <int> ();
If you cannot modify Foo. cpp, just create a new. cpp file, for example, the foo-impl.cpp is as follows:
// File "foo-impl.cpp"
# Include "foo. cpp"
Template void Foo <int> ();
Note that the foo-impl.cpp file contains the. cpp file, not the. h file. If you are confused, dance a tap, think about Kansas, and repeat with me, "I want to do this, even if it is messy ." You need to trust me. If you do not trust or are curious, the previous FAQ provides reasons.