|
-
February 20th, 2003, 09:07 AM
#1
Question about overloading operator >> in STL.
Hi, everyone!
I meet with some trouble when debugging the following
codes dealing with overloading operator >> in STL.
I have tried my best to resolve it but still failed.
My IDE is VC6.0.
Source Codes:
Code:
--------
#include <iostream>
#include <map>
#include <set>
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;
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<set <employee, less<employee> >::iterator, bool>
result = employee_set.insert (e1);
if (result.second) cout << "insert ok"; else cout << "not inserted";
cout << endl << (*result.first).description.name << endl;
result = employee_set.insert (e1);
if (result.second) cout << "insert ok"; else cout << "not inserted";
return 1;
}
--------
Error messages:
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(22) : error C2679: binary '<<' : no operator
defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> >' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(56) : error C2679: binary '<<' : no operator
defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<
char> >' (or there is no acceptable conversion)
--------
How to resolve them?
Thanks in advance,
George
[Gabriel: added CODE tags]
Last edited by Gabriel Fleseriu; February 20th, 2003 at 09:24 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|