CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Unhappy Question about ambiguous overloading operator >> in STL.

    Hi, everyone!


    Please look at the following codes. I think I overload the
    operator clearly. But compiler still say that it is ambiguous.
    Here are the source codes and related error message.


    Source:

    --------
    #include <iostream>
    #include <map>
    #include <set>
    #include <string>

    using namespace std;

    class employee_data {
    public:
    employee_data() : name (""), skill(0), salary(0) {}
    employee_data(string n, int s, long sa) :
    name (n), skill (s), salary (sa) {}

    string name;
    int skill;
    long salary;

    friend ostream& operator<< (ostream& os, const employee_data& e);
    };

    ostream& operator<< (ostream& os, const employee_data& e) {
    os << "employee: " << e.name << " " << e.skill << " " << e.salary;
    return os;
    }

    class employee {
    public:
    employee (int i, employee_data e) :
    identification_code (i), description (e) {}

    int identification_code; // key expression to identify an employee
    employee_data description;

    bool operator< (const employee& e) const {
    return identification_code < e.identification_code; }
    };

    int main()
    {
    set <employee, less<employee> > employee_set;

    multiset <employee, less<employee> > employee_multiset;

    map <int, employee_data, less<int> > employee_map;
    multimap <int, employee_data, less<int> > employee_multimap;

    employee_data ed1 ("john", 1, 5000);
    employee_data ed2 ("tom", 5, 2000);
    employee_data ed3 ("mary", 2, 3000);

    employee e1 (1010, ed1);
    employee e2 (2020, ed2);
    employee e3 (3030, ed3);

    pair<const int, employee_data> a[2] = { make_pair (2020, ed2),
    make_pair (3030, ed3) };
    employee_map.insert (&a[0], &a[2]);

    employee_data d = employee_map[2020];
    cout << d << endl;


    return 1;
    }
    --------


    Error message:
    --------
    C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(86) : error C2593: 'operator <<' is ambiguous
    --------


    Thanks in advance,
    George

  2. #2
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468
    Hi, everyone!

    I have tried it in VC7.0 and Linux gcc. Both OK, something
    wrong with VC6.0?


    George

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured