what does g++ want from my code

what does g++ want from my code

Dov Grobgeld dov.grobgeld at gmail.com
Wed Aug 25 12:42:27 IDT 2010


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 <erez0001 at gmail.com>

>
>
> On Wed, Aug 25, 2010 at 12:08 PM, Elazar Leibovich <elazarl at 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 <erez0001 at 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 at cs.huji.ac.il
>>> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
>>>
>>>
>>
>
> _______________________________________________
> Linux-il mailing list
> Linux-il at cs.huji.ac.il
> http://mailman.cs.huji.ac.il/mailman/listinfo/linux-il
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.cs.huji.ac.il/pipermail/linux-il/attachments/20100825/8b59e69b/attachment-0001.html>


More information about the Linux-il mailing list