Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplate function giving compilation error
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3149859/stdmapconst-iterator-template-compilation-error">std::map::const_iterator template compilation error</a> </p> </blockquote> <p>The idea is to create a function which takes the container type as a Template parameter. Since maps have to summed up differently compared to other sequential containers I overloaded the Sum function for the map as shown below.</p> <p>This is the function which is giving me errors:</p> <pre><code>template&lt;typename T1, typename T2&gt; double Sum(const map&lt;T1,T2&gt;&amp; input) { double finalSum=0; map&lt;T1,T2&gt;::const_iterator iter_begin=input.begin(); map&lt;T1,T2&gt;::const_iterator iter_end=input.end(); for(iter_begin; iter_begin!=iter_end; ++iter_begin) { finalSum=finalSum+(iter_begin)-&gt;second; } return finalSum; } </code></pre> <p>Error:</p> <p>1>c:\documents and settings\keep\my documents\visual studio 2010\projects\level 7\exercise 2\exercise 2\sum.h(34): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int</p> <p>1>c:\documents and settings\keep\my documents\visual studio 2010\projects\level 7\exercise 2\exercise 2\sum.h(34): error C2143: syntax error : missing ',' before '&lt;'</p> <p>This function is a part of the header file. My header file includes the function definitions also.</p> <p>Few things: 1. I tried with typename but I might have been wrong. Templates are not my strong area yet. Feel free to point if typename is needed somewhere. Inline keyword will help?</p> <ol start="2"> <li><strong>The same code is compiling fine on my Guide's machine.</strong> Mine is VC++ 2010 Express SP1. I do not know what his version of VC++ is.</li> </ol> <p>Thanks in advance.</p> <p><strong>EDIT: Posting the whole Header file.With typename addition as suggested</strong>. But same error. The first version of Sum is compiling fine without typename. The second one overloaded for map is giving issues.</p> <pre><code>#ifndef SUM_H #define SUM_H template&lt;typename T&gt; double Sum(const T&amp; input) { double finalSum=0; T::const_iterator iter_begin=input.begin(); T::const_iterator iter_end=input.end(); for(iter_begin; iter_begin!=iter_end; ++iter_begin) { finalSum=finalSum+(*iter_begin); } return finalSum; } //Mysterion !!!!! template&lt;typename T1, typename T2&gt; double Sum(const map&lt;T1,T2&gt;&amp; input) { double finalSum=0; typename map&lt;T1,T2&gt;::const_iterator iter_begin=input.begin(); typename map&lt;T1,T2&gt;::const_iterator iter_end=input.end(); for(iter_begin; iter_begin!=iter_end; ++iter_begin) { finalSum=finalSum+(iter_begin)-&gt;second; } return finalSum; } #endif </code></pre> <p>The error is coming at: double Sum(const map&amp; input)</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