Note that there are some explanatory texts on larger screens.

plurals
  1. POCompiler warning at C++ template base class
    primarykey
    data
    text
    <p>I get a compiler warning, that I don't understand in that context. When I compile the "Child.cpp" from the following code. (Don't wonder: I stripped off my class declarations to the bare minimum, so the content will not make much sense, but you will see the problem quicker). I get the warning with <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_.NET_2003" rel="nofollow noreferrer">Visual&nbsp;Studio&nbsp;2003</a> and <a href="http://en.wikipedia.org/wiki/Microsoft_Visual_Studio#Visual_Studio_2008" rel="nofollow noreferrer">Visual&nbsp;Studio&nbsp;2008</a> on the highest warning level.</p> <hr> <h2>The code</h2> <p><strong>AbstractClass.h:</strong></p> <pre><code>#include &lt;iostream&gt; template&lt;typename T&gt; class AbstractClass { public: virtual void Cancel(); // { std::cout &lt;&lt; "Abstract Cancel" &lt;&lt; std::endl; }; virtual void Process() = 0; }; // Outside definition. If I comment out this and take the inline // definition like above (currently commented out), I don't get // a compiler warning. template&lt;typename T&gt; void AbstractClass&lt;T&gt;::Cancel() { std::cout &lt;&lt; "Abstract Cancel" &lt;&lt; std::endl; } </code></pre> <p><strong>Child.h:</strong></p> <pre><code>#include "AbstractClass.h" class Child : public AbstractClass&lt;int&gt; { public: virtual void Process(); }; </code></pre> <p><strong>Child.cpp:</strong></p> <pre><code>#include "Child.h" #include &lt;iostream&gt; void Child::Process() { std::cout &lt;&lt; "Process" &lt;&lt; std::endl; } </code></pre> <hr> <h2>The warning</h2> <p>The class "Child" is derived from "AbstractClass". In "AbstractClass" there's the public method "AbstractClass::Cancel()". If I define the method outside of the class body (like in the code you see), I get the compiler warning...</p> <blockquote> <p>AbstractClass.h(7) : warning C4505: 'AbstractClass::Cancel' : unreferenced local function has been removed with [T=int]</p> </blockquote> <p>...when I compile "Child.cpp". I do not understand this, because this is a <em>public</em> function, and the compiler can't know if I later reference this method or not. And, in the end, I reference this method, because I call it in main.cpp and despite this compiler warning, this method works if I compile and link all files and execute the program:</p> <pre><code>//main.cpp #include &lt;iostream&gt; #include "Child.h" int main() { Child child; child.Cancel(); // Works, despite the warning } </code></pre> <p>If I do define the Cancel() function as inline (you see it as out commented code in AbstractClass.h), then I don't get the compiler warning. Of course my program works, but I want to understand this warning or is this just a compiler mistake?</p> <p>Furthermore, if do not implement AbsctractClass as a template class (just for a test purpose in this case) I also don't get the compiler warning...?</p> <hr> <p>If I make a non-virtual function, I don't get the compile warning for that non-virtual function, but all answers up to now don't comprise the virtual stuff. Try this:</p> <pre><code>template&lt;typename T&gt; class AbstractClass { public: virtual void Cancel(); // { std::cout &lt;&lt; "Abstract Cancel" &lt;&lt; std::endl; }; virtual void Process() = 0; void NonVirtualFunction(); }; //... template&lt;typename T&gt; void AbstractClass&lt;T&gt;::NonVirtualFunction() { std::cout &lt;&lt; "NonVirtualFunction" &lt;&lt; std::endl; } </code></pre> <p>The answers up to know helped me, but I don't think that the question is fully answered.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload