Problem with namespace while overloading operator< for stl::map

Discussion in 'C++' started by Ytsen, Jan 15, 2009.

  1. Ytsen

    Ytsen New Member

    Joined:
    Jan 15, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Dear reader,

    The following minimal program compiles and runs properly (note the commented out lines concerning the namespaces):
    Code:
    #include <iostream>
    #include <map>
    
    using namespace std;
    
    //namespace k {
    
    class TKey {
    
    public:
    TKey(int a ) : fA(a) {};
    
    int GetA() const { return fA; };
    
    private:
    int fA;
    };
    
    //};
    
    //using namespace k;
    
    
    bool operator<(const TKey & k1, const TKey & k2) { return k1.GetA() < k2.GetA(); };
    
    int main ()
    {
    
    // Construct TKey
    TKey k1(3);
    
    // Declare my map and its iterator
    map<TKey,int> myMap;
    map<TKey,int>::iterator it;
    
    // Insert k1 into the map
    myMap.insert( pair<TKey,int>(k1,k1.GetA()));
    
    // Put out content of the map showing all is fine:
    cout << "myMap contains:\n";
    for ( it=myMap.begin() ; it != myMap.end(); it++ )
    cout << it->first.GetA() << " => " << it->second << endl;
    
    // Done
    return 0;
    };
    
    
    The output is, as you can check yourself easily:

    myMap contains:
    3 => 3

    However, when I now comment in the commented-out lines, I get the following error:

    /usr/include/c++/4.3/bits/stl_function.h: In member function ‘bool std::less<_Tp>::eek:perator()(const _Tp&, const _Tp&) const [with _Tp = k::TKey]’:
    /usr/include/c++/4.3/bits/stl_tree.h:1141: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = k::TKey, _Val = std::pair<const k::TKey, int>, _KeyOfValue = std::_Select1st<std::pair<const k::TKey, int> >, _Compare = std::less<k::TKey>, _Alloc = std::allocator<std::pair<const k::TKey, int> >]’
    /usr/include/c++/4.3/bits/stl_map.h:469: instantiated from ‘std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename _Alloc::rebind<std::pair<const _Key, _Tp> >::eek:ther>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std::pair<const _Key, _Tp>&) [with _Key = k::TKey, _Tp = int, _Compare = std::less<k::TKey>, _Alloc = std::allocator<std::pair<const k::TKey, int> >]’
    map2.C:37: instantiated from here
    /usr/include/c++/4.3/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’


    Here is get a bit puzzled, can anyone help me understand what is going on here?

    Many thanks in advance,

    Ytsen.
     
  2. Ytsen

    Ytsen New Member

    Joined:
    Jan 15, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Problem solved thanks to Icon: forums devarticles com slesh member php ? u=23135


    "Include the operator< inside the namespace. That is where it semantically belongs anyway, I guess. I think the issue is that the point where operator< is needed is inside the stl somewhere (<functional> in my case). This stl file does not have 'using namespace k' of course. That is why it cannot find a suitable operator<, if you add operator< to the namespace then 'argument-dependent-lookp' will make sure the operator< is found.

    Google for ADL or Koenig-lookup to find out more about that."
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice