<div dir="ltr">The following works at least in g++ 4.4.4:<br>
<meta name="qrichtext" content="1"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><style type="text/css">
p, li { white-space: pre-wrap; }
</style>
<pre style="margin: 12px 0px 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">#include <map></span></pre>
<pre style="margin: 0px; text-indent: 0px; font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255); background-color: rgb(255, 255, 255);"></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);">template</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> <typename a, typename b></span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);">class</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> c {</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);">private</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);">:</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> b defaultVal;</span></pre>
<pre style="margin: 0px; text-indent: 0px; font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);"></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);">public</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);">:</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> std::map<a,b> mymap;</span></pre>
<pre style="margin: 0px; text-indent: 0px; font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);"></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">const</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> b &func(a idx)</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> {</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><b><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);">auto</span></b><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> it=mymap.find(idx);</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">if</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> (it!=mymap.end())</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">return</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> it->second;</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">else</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> </span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 255);">return</span><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> defaultVal;</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"> }</span></pre>
<pre style="margin: 0px; text-indent: 0px; background-color: rgb(255, 255, 255);"><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);">};</span></pre>
<pre style="margin: 0px 0px 12px; text-indent: 0px; background-color: rgb(255, 255, 255);"> </pre>Note the<b><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(43, 145, 175);"> auto</span></b><span style="font-family: 'Courier New'; font-size: 10pt; color: rgb(0, 0, 0);"></span> keyword that is a C++0x extension. You'll have to compile with the -std=gnu++0x flag.<br>
<br>See: <a href="http://gcc.gnu.org/projects/cxx0x.html">http://gcc.gnu.org/projects/cxx0x.html</a><br><br>Regards,<br>Dov<br><br><div class="gmail_quote">2010/8/25 Erez D <span dir="ltr"><<a href="mailto:erez0001@gmail.com">erez0001@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div dir="ltr"><br><br><div class="gmail_quote"><div class="im">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="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; 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 class="h5"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; 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="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; 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>
<br>_______________________________________________<br>
Linux-il mailing list<br>
<a href="mailto:Linux-il@cs.huji.ac.il">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></blockquote></div><br></div>