0

Why this code compiles succesfully in VS13 and fails to compile by gcc?

template <typename T>    
class Base
{
protected:
    typedef std::map<T, double> MyMap;
    MyMap m_map;

public:
    void func(const T& key)
    {
        typename MyMap::iterator it = m_map.find(key);
        if(it != m_map.end()) {
            // ....
        }
    }
};

class Inherited1 : public Base <char>
{ };
class Inherited2 : public Base <int>
{ };

It results in following errors (gcc 4.1.2)

test.cpp: In member function ‘void Base<T>::func(const T&)’:
test.cpp:16: error: expected `;' before ‘it’
test.cpp:17: error: ‘it’ was not declared in this scope

EDIT1: Thanks for replies. Tried using keyword typename as juanchopanza suggested, now I got this errors:

/tmp/ccotJWFl.o: In function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, double> > >::deallocate(std::_Rb_tree_node<std::pair<int const, double> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKidEEE10deallocateEPS5_m[__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<int const, double> > >::deallocate(std::_Rb_tree_node<std::pair<int const, double> >*, unsigned long)]+0x19): undefined reference to `operator delete(void*)'
/tmp/ccotJWFl.o: In function `__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<char const, double> > >::deallocate(std::_Rb_tree_node<std::pair<char const, double> >*, unsigned long)':
test.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKcdEEE10deallocateEPS5_m[__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<char const, double> > >::deallocate(std::_Rb_tree_node<std::pair<char const, double> >*, unsigned long)]+0x19): undefined reference to `operator delete(void*)'
/tmp/ccotJWFl.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'

EDIT2: This whole stuff becomes even more ridiculous - it results in the above mentioned errors if I use gcc test.cpp compiles successfully if I write g++ test.cpp

Vram Vardanian
  • 539
  • 1
  • 5
  • 15

0 Answers0