The following works at least in g++ 4.4.4: #include <map>
template <typename a, typename b> class c { private: b defaultVal; public: std::map<a,b> mymap; const b &func(a idx) { *auto* it=mymap.find(idx); if (it!=mymap.end()) return it->second; else return defaultVal; } }; Note the* auto* keyword that is a C++0x extension. You'll have to compile with the -std=gnu++0x flag. See: http://gcc.gnu.org/projects/cxx0x.html Regards, Dov 2010/8/25 Erez D <erez0...@gmail.com> > > > On Wed, Aug 25, 2010 at 12:08 PM, Elazar Leibovich <elaz...@gmail.com>wrote: > >> template <class a, class b> >> >> class c >> >> { >> >> private: >> >> b defaultVal; >> >> public: >> >> std::map<a,b> mymap; >> >> const b &func(a idx) >> >> { >> >> typename std::map<a,b>::iterator it(mymap.find(idx)); >> >> if (it!=mymap.end()) >> >> return it->second; >> >> else >> >> return defaultVal; >> >> } >> >> }; >> >> >> The compiler is not sure std::map<a,b>::iterator is a typename or a value. >> > how do i sove that ? can you give a solution ? > >> >> 2010/8/25 Erez D <erez0...@gmail.com> >> >>> hi >>> >>> >>> i am compiling the following code: >>> >>> 1:#include <map> >>> 2: >>> 3:template <class a, class b> >>> 4:class c >>> 5:{ >>> 6: private: >>> 7: b defaultVal; >>> 8: public: >>> 9: std::map<a,b> mymap; >>> 10: const b &func(a idx) >>> 11: { >>> 12: std::map<a,b>::iterator it=mymap.find(a); >>> 13: if (it!=mymap.end()) >>> 14: return it->second; >>> 15: else >>> 16: return defaultVal; >>> 17: } >>> 18:}; >>> ... >>> ... >>> >>> compiling it (under cygwin) , i get the following: >>> >>> $ g++ -c -o kaka.o kaka.cpp >>> kaka.cpp: In member function 'const b& c<a, b>::func(a)': >>> kaka.cpp:12: error: expected `;' before 'it' >>> kaka.cpp:13: error: 'it' was not declared in this scope >>> >>> >>> what's wrong ? >>> >>> _______________________________________________ >>> Linux-il mailing list >>> Linux-il@cs.huji.ac.il >>> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il >>> >>> >> > > _______________________________________________ > Linux-il mailing list > Linux-il@cs.huji.ac.il > http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il > >
_______________________________________________ Linux-il mailing list Linux-il@cs.huji.ac.il http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il