Home >Backend Development >C++ >Why Do I Get 'Undefined Reference to' Errors for Template Class Constructors, and How Can I Fix Them?
The compiler error "undefined reference to..." for template class constructors signifies a lack of explicit instantiation instructions for the specific template classes used in the program. This issue arises because the compiler doesn't automatically compile template code until it's explicitly required.
Append explicit template instantiation statements to the end of the implementation file (cola.cpp in this case):
template class cola<float>; template class cola<string>;
Additionally, include the following lines in nodo_colaypila.cpp:
template class nodo_colaypila<float>; template class nodo_colaypila<std::string>;
This forces the compiler to compile the required template classes.
Move the implementation code from cola.cpp and nodo_colaypila.cpp into cola.h and nodo_colaypila.h, respectively. This ensures the availability of function definitions in all translation units using the template classes.
Solution 1:
Solution 2:
nodo_colaypila<T><T>* ult, pri;
should be:
nodo_colaypila<T>* ult, *pri;
The above is the detailed content of Why Do I Get 'Undefined Reference to' Errors for Template Class Constructors, and How Can I Fix Them?. For more information, please follow other related articles on the PHP Chinese website!