<div dir="ltr">For further explanation about the need for typename look at <div><a href="http://www.parashift.com/c++-faq-lite/templates.html#35.18">http://www.parashift.com/c++-faq-lite/templates.html#35.18</a></div><div>
<a href="http://www.parashift.com/c++-faq-lite/templates.html#35.18"></a>Item 35.18<br><br><div class="gmail_quote">On Wed, Aug 25, 2010 at 12:33 PM, Erez D <span dir="ltr"><<a href="mailto:erez0001@gmail.com" target="_blank">erez0001@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><br><div class="gmail_quote"><div>On Wed, Aug 25, 2010 at 12:08 PM, Elazar Leibovich <span dir="ltr"><<a href="mailto:elazarl@gmail.com" target="_blank">elazarl@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204, 204, 204);padding-left:1ex">
<div dir="ltr"><div><span style="color:rgb(128, 128, 0)">template</span><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(0, 0, 0)"><</span><span style="color:rgb(128, 128, 0)">class</span><span style="color:rgb(192, 192, 192)"> </span>a<span style="color:rgb(0, 0, 0)">,</span><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">class</span><span style="color:rgb(192, 192, 192)"> </span>b<span style="color:rgb(0, 0, 0)">></span>
</div><pre style="margin:0px;text-indent:0px"><span style="color:rgb(128, 128, 0)">class</span><span style="color:rgb(192, 192, 192)"> </span>c</pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(0, 0, 0)">{</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">private</span><span style="color:rgb(0, 0, 0)">:</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span>b<span style="color:rgb(192, 192, 192)"> </span>defaultVal<span style="color:rgb(0, 0, 0)">;</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">public</span><span style="color:rgb(0, 0, 0)">:</span></pre><div>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span>std<span style="color:rgb(0, 0, 0)">::</span>map<span style="color:rgb(0, 0, 0)"><</span>a<span style="color:rgb(0, 0, 0)">,</span>b<span style="color:rgb(0, 0, 0)">></span><span style="color:rgb(192, 192, 192)"> </span>mymap<span style="color:rgb(0, 0, 0)">;</span></pre>
</div><pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><div><span style="color:rgb(128, 128, 0)">const</span><span style="color:rgb(192, 192, 192)"> </span>b<span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(0, 0, 0)">&</span>func<span style="color:rgb(0, 0, 0)">(</span>a<span style="color:rgb(192, 192, 192)"> </span>idx<span style="color:rgb(0, 0, 0)">)</span></div>
</pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(0, 0, 0)">{</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">typename</span><span style="color:rgb(192, 192, 192)"> </span>std<span style="color:rgb(0, 0, 0)">::</span>map<span style="color:rgb(0, 0, 0)"><</span>a<span style="color:rgb(0, 0, 0)">,</span>b<span style="color:rgb(0, 0, 0)">>::</span>iterator<span style="color:rgb(192, 192, 192)"> </span>it<span style="color:rgb(0, 0, 0)">(</span>mymap<span style="color:rgb(0, 0, 0)">.</span>find<span style="color:rgb(0, 0, 0)">(</span>idx<span style="color:rgb(0, 0, 0)">));</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">if</span><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(0, 0, 0)">(</span>it<span style="color:rgb(0, 0, 0)">!=</span>mymap<span style="color:rgb(0, 0, 0)">.</span>end<span style="color:rgb(0, 0, 0)">())</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">return</span><span style="color:rgb(192, 192, 192)"> </span>it<span style="color:rgb(0, 0, 0)">-></span>second<span style="color:rgb(0, 0, 0)">;</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">else</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(128, 128, 0)">return</span><span style="color:rgb(192, 192, 192)"> </span>defaultVal<span style="color:rgb(0, 0, 0)">;</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(192, 192, 192)"> </span><span style="color:rgb(0, 0, 0)">}</span></pre>
<pre style="margin:0px;text-indent:0px"><span style="color:rgb(0, 0, 0)">};</span></pre><pre style="margin:0px;text-indent:0px"><span style="color:rgb(0, 0, 0)"><br></span></pre>The compiler is not sure std::map<a,b>::iterator is a typename or a value.</div>
</blockquote></div><div>how do i sove that ? can you give a solution ? <br></div><div><div></div><div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204, 204, 204);padding-left:1ex">
<div dir="ltr"><div>
<br><div class="gmail_quote"><div>2010/8/25 Erez D <span dir="ltr"><<a href="mailto:erez0001@gmail.com" target="_blank">erez0001@gmail.com</a>></span><br>
</div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204, 204, 204);padding-left:1ex"><div dir="ltr">hi<div><div></div><div><br><br>i am compiling the following code:<br>
<br>1:#include <map><br>2:<br>3:template <class a, class b><br>
4:class c<br>5:{<br>6: private:<br>7: b defaultVal;<br>8: public:<br>
9: std::map<a,b> mymap;<br>10: const b &func(a idx)<br>11: {<br>12: std::map<a,b>::iterator it=mymap.find(a);<br>13: if (it!=mymap.end())<br>
14: return it->second;<br>15: else<br>16: return defaultVal;<br>17: }<br>18:};<br>...<br>...<br><br>compiling it (under cygwin) , i get the following:<br>
<br>$ g++ -c -o kaka.o kaka.cpp<br>kaka.cpp: In member function 'const b& c<a, b>::func(a)':<br>kaka.cpp:12: error: expected `;' before 'it'<br>kaka.cpp:13: error: 'it' was not declared in this scope<br>
<br><br>what's wrong ?<br></div></div></div>
<br><div>_______________________________________________<br>
Linux-il mailing list<br>
<a href="mailto:Linux-il@cs.huji.ac.il" target="_blank">Linux-il@cs.huji.ac.il</a><br>
<a href="http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il" target="_blank">http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il</a><br>
<br></div></blockquote></div><br></div></div>
</blockquote></div></div></div><br></div>
</blockquote></div><br></div></div>