Linker error 'unresolved external symbol' : working with templates

穿精又带淫゛_ 提交于 2019-12-04 03:56:26

You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.

Generally speaking it is considered best practice to write your template code entirely inside header files. There is an important technical reason for this: when you instantiate a template, the C++ compiler needs to generate code from that template that is specific to the template parameters that you have specified. If your template code is placed entirely in your headers, this is done for you automatically.

It is definitely possible to write template code the way that you have, with the implementation placed in cpp files. If you do this, however, you are required to explicitly instantiate the template instance that you intend to use.

In your case, you need to add the following line to a .cpp file in your project:

template class Allotter<QTcpSocket>;

Since you can't place template implementation in .cpp files, it is considered a good practice to use .inl files for the template implementation and include them from the template headers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!