“Undefined symbols” linker error with simple template class

有些话、适合烂在心里 提交于 2019-11-27 04:50:50

The template definition (the cpp file in your code) has to be included prior to instantiating a given template class, so you either have to include function definitions in the header, or #include the cpp file prior to using the class (or do explicit instantiations if you have a limited number of them).

Including the cpp file containing the implementations of the template class functions works. However, IMHO, this is weird and awkward. There must surely be a slicker way of doing this?

If you have only a few different instances to create, and know them beforehand, then you can use "explicit instantiation"

This works something like this:

At the top of gene.cpp add the following lines

template class Gene<int>;
template class Gene<float>;
Brian

In if(value >= this->minValue && value <= this->minValue) the second minValue should be maxValue, no?

Echo what Sean said: What's the error message? You've defined and declared the functions, but you've not used them in anything anywhere, nor do I see an error (besides the typo).

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