Alain Li Log in 2006-11-29 11:58 problem
Grabword error LNK2019: unresolved external symbol
"Void __cdecl gettextfromtextouthook<unsigned Short const *> (int,struct hdc__ *,int,int,unsigned Short const *,int , int const *) "(?? $GetTextFromTextOutHook @pbg@ @YAXHPAUHDC__ @ @HHPBGHPBH @z), the symbol is referenced in the function _nhtextoutw@20
In NHEXTTEXTOUTA,NHEXTTEXTOUTW not prompt, error in nhtextouta,nhtextout.
INT nDx = 0;
Gettextfromtextouthook<lpcwstr> (FALSE, HDC, nxstart,nystart,lpstring,cbstring,&ndx); Problem Analysis: Templates need to be pre-compiled once call file----Test.cpp
#include <windows.h>
#include <stdio.h>
#include "temp.h"
void Main ()
{
ftemp< int> (1000);//calling template functions in other files will cause problems
//printf ("%d", Other (122200));//calling non-template functions does not cause problems,
}
template function declaration--------Temp.h
#pragma once
template <class type>
int ftemp (Type I);
int test ();
int other (int t);
template function definition---------temp.cpp-
#include "header.h"
template <class type>
int ftemp (Type I)
{
return i;
}
int Test ()
{
return ftemp<int> (0);
}
int other (int t)
{
return t;
}
Analysis----------------------
First, the program compiles from the main file test, compiles the temp.h, but when it encounters a template, it reserves the creation of a symbol that does not ftemp the templates function.
Therefore, if you call ftemp directly, you will see a phenomenon where function symbols cannot be found.
However, if you define a function test in temp.h, using the template ftemp function in this function--the function test does not have to be called, just to generate the Ftemp function symbol for the stencil. This makes it possible to use the template function ftemp.
If the template function is defined in the header file Temp.h, not just the declaration, then in the test.cpp can also invoke templates.
All I do is use a function that does not call test, so there is no problem with the "unresolved external symbols" solution template Call problem method
Add a function that is not called in the file where the Gettextfromtextouthook function is located
void Test ()
{
gettextfromtextouthook<lpctstr> (TRUE, NULL, 0,0, "0,null");
}
Analysis Data C + + Primer Third Edition Chinese version 10.5 Template Compilation mode:
"C + + supports two template compilation modes including mode inclusion model and detach mode separation model" 10.5.1 contains compilation mode
"In the included compilation mode we include the definition of the function template in the file that each template is instantiated in and tend to place the definition in the header file as in the inline function" 10.5.2 separate compilation mode
"The Declaration of a function template is placed in the header file in separate compilation mode"
"There is a keyword in the template definition export"
The keyword export tells the compiler that this template definition may be required when generating instances of function templates that are used by other files, and that the template definition must be guaranteed to be visible when these instances are generated.
"Keyword export does not need to appear in the template declaration of the header file"
"Separation mode allows us to properly separate the interface of a function template from its implementation and organize the program so that the interface of the function template is placed in the header file and the implementation is placed in a text file but not all compilers support the separation mode even though support does not always support the separation mode requires a more complex programming environment so they cannot be in all C The + + compiler implementation provides "
"Inside the C + + Object model describes a C + + compiler supported template instantiation mechanism for the Edison Design Group compiler" does not support detach mode
Unfortunately, any version of VC currently (Visual Studio 2005 unknown) does not support detach mode.
1, the first definition of the template is not a function definition, they are just compiler directives, explain how to generate functions or classes, so separate them into another file, and can not be compiled separately; write the implementation to the. h file.
2, if the currently used compiler implements the Export keyword, you can put the definition of the template in another file, but the template function definition is similar to the following:
Export Template<typename t>
void Setobj (t const& t) {m_obj = t;}
As long as the template, declaration and definition will be placed in the header file.VC does not support export keyword unless special
In this case, the template function is not a simple function, so be sure to adapt to alternative methods, not using complex functions and procedures in template functions.