<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[unresolved external symbol  error]]></title><description><![CDATA[<p dir="auto">Hello everyone. I'm looking for help to debug my Qt program (Console Application on Windows).<br />
/********************************************************<br />
This function is used to check whether the number passed in parameter is a perfect n-th power or not.<br />
By default, n=2 (then checks if the number is a perfect square).<br />
*******************************************************/<br />
template&lt;typename T&gt;<br />
bool Numbers::isPerfectPower(const T&amp; number, const int &amp;n)<br />
{<br />
if (number &lt; 0 &amp;&amp; n % 2 == 0)<br />
return false;<br />
T epsilon = std::numeric_limits&lt;T&gt;::epsilon();<br />
T i = 0;<br />
while (std::pow(i, n) &lt;= number)<br />
{<br />
double result = std::pow(i, n);<br />
if (std::fabs(result - number) &lt; epsilon)<br />
return true;<br />
i += epsilon;<br />
}<br />
return false;<br />
}</p>
<p dir="auto">In "Numbers.h", I wrote:</p>
<p dir="auto">namespace Numbers<br />
{<br />
//...<br />
template&lt;typename T&gt;<br />
bool isPerfectPower(const T&amp; number, const int &amp;n=2);</p>
<p dir="auto">}</p>
<p dir="auto">And in the main.cpp file:</p>
<p dir="auto">#include &lt;QCoreApplication&gt;<br />
#include "Numbers.h"<br />
//...<br />
using namespace Numbers;</p>
<p dir="auto">int main(int argc, char *argv[])<br />
{<br />
QCoreApplication a(argc, argv);<br />
std::cout&lt;&lt;"\n\t"&lt;&lt;isPerfectPower(16.0,2);</p>
<p dir="auto">return a.exec();</p>
<p dir="auto">}</p>
<p dir="auto">Problem, I have the compilation error:</p>
<p dir="auto">main.obj:-1: error : LNK2019: unresolved external symbol "bool __cdecl Numbers::isPerfectPower&lt;double&gt;(double const &amp;,int const &amp;)" (??$isPerfectPower@N@Numbers@@YA_NAEBNAEBH@Z) referenced in main function<br />
Thanks in advance.</p>
]]></description><link>https://forum.qt.io/topic/146160/unresolved-external-symbol-error</link><generator>RSS for Node</generator><lastBuildDate>Sun, 06 Sep 2026 23:56:19 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/146160.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 Jul 2023 21:08:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to unresolved external symbol  error on Sun, 02 Jul 2023 23:25:10 GMT]]></title><description><![CDATA[<p dir="auto">Hi <a class="plugin-mentions-user plugin-mentions-a" href="/user/bineere">@<bdi>Bineere</bdi></a>,</p>
<p dir="auto">While defining (not just declaring) the template function in the header, as <a class="plugin-mentions-user plugin-mentions-a" href="/user/chris-kawa">@<bdi>Chris-Kawa</bdi></a>  suggested, is the easier, more generalised solution, if you do need / want to keep the implementation private, you can provide <a href="https://en.cppreference.com/w/cpp/language/function_template#Explicit_instantiation" target="_blank" rel="noopener noreferrer nofollow ugc">explicit instantiations</a> within your Numbers translation unit by adding something like this to the end of your <code>Numbers.cpp</code> file:</p>
<pre><code>template bool Numbers::isPerfectPower(const double &amp;number, const int &amp;n);
</code></pre>
<p dir="auto">But then you have to do this for all <code>typename T</code> types you want to support, eg:</p>
<pre><code>// Explicit template instantiations.
template bool Numbers::isPerfectPower(const float &amp;number, const int &amp;n);
template bool Numbers::isPerfectPower(const double &amp;number, const int &amp;n);
template bool Numbers::isPerfectPower(const long double &amp;number, const int &amp;n);
</code></pre>
<p dir="auto">If you wanted to support integers, you'd have to add explicit instantiations for those too, but since you're using <code>std::numeric_limits&lt;T&gt;::epsilon()</code>, I assume you're happy to only support floating point types for now.</p>
<p dir="auto">Cheers.</p>
]]></description><link>https://forum.qt.io/post/763376</link><guid isPermaLink="true">https://forum.qt.io/post/763376</guid><dc:creator><![CDATA[Paul Colby]]></dc:creator><pubDate>Sun, 02 Jul 2023 23:25:10 GMT</pubDate></item><item><title><![CDATA[Reply to unresolved external symbol  error on Sun, 02 Jul 2023 22:03:35 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/bineere">@<bdi>Bineere</bdi></a> Templates are instantiated at the place of usage, so their body needs to be available there. The error says it can't find the specialization of your template for the double type. That's because compiler can't create it at the place of instantiation because you moved the body of the function to the cpp file.</p>
<p dir="auto">In short template functions code should go all in the header file and not be split between h and cpp.</p>
]]></description><link>https://forum.qt.io/post/763367</link><guid isPermaLink="true">https://forum.qt.io/post/763367</guid><dc:creator><![CDATA[Chris Kawa]]></dc:creator><pubDate>Sun, 02 Jul 2023 22:03:35 GMT</pubDate></item></channel></rss>