Click to See Complete Forum and Search --> : Question about overloading operator >> in STL.


George2
February 20th, 2003, 08:07 AM
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:

--------
#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]

Philip Nicoletti
February 20th, 2003, 08:24 AM
you forgot an include :


#include <string>

George2
February 20th, 2003, 08:31 AM
Thanks, Philip buddie!

I have tried your method. There are no errors but several warning informations. I doubted why the length of characters
is limited. Can you help?

George
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(62) : warning C4786: 'std::pair<std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee
> >::iterator,std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::iterator>' : identifier was truncated to '255' characters in the debug information
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(62) : warning C4786: 'std::pair<std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee
> >::const_iterator,std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::const_iterator>' : identifier was truncated to '255' characters in the debug info
rmation
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(62) : warning C4786: 'std::pair<std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<emp
loyee> >::iterator,std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::iterator>' : identifier was truncated to '255' characters in the debug inform
ation
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(62) : warning C4786: 'std::pair<std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<emp
loyee> >::const_iterator,std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::const_iterator>' : identifier was truncated to '255' characters in the
debug information
c:\program files\microsoft visual studio\vc98\include\xtree(182) : warning C4786: 'std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::~_Tree<employee,em
ployee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\xtree(182) : warning C4786: 'std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::~_Tree<employ
ee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\xtree(162) : warning C4786: 'std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::_Tree<employee,emp
loyee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\xtree(236) : warning C4786: '__ehhandler$?insert@?$_Tree@Vemployee@@V1@U_Kfn@?$set@Vemployee@@U?$less@Vemployee@@@std@@V?$allocator@Vemployee@@@3@@std@@U?$less@Vemployee@@@4@V?$allocator@Vemploye
e@@@4@@std@@QAE?AU?$pair@Viterator@?$_Tree@Vemployee@@V1@U_Kfn@?$set@Vemployee@@U?$less@Vemployee@@@std@@V?$allocator@Vemployee@@@3@@std@@U?$less@Vemployee@@@4@V?$allocator@Vemployee@@@4@@std@@_N@2@ABVemployee@@@Z' : identifier was truncated to '255
' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\xtree(236) : warning C4786: '__unwindfunclet$?insert@?$_Tree@Vemployee@@V1@U_Kfn@?$set@Vemployee@@U?$less@Vemployee@@@std@@V?$allocator@Vemployee@@@3@@std@@U?$less@Vemployee@@@4@V?$allocator@Vemp
loyee@@@4@@std@@QAE?AU?$pair@Viterator@?$_Tree@Vemployee@@V1@U_Kfn@?$set@Vemployee@@U?$less@Vemployee@@@std@@V?$allocator@Vemployee@@@3@@std@@U?$less@Vemployee@@@4@V?$allocator@Vemployee@@@4@@std@@_N@2@ABVemployee@@@Z$0' : identifier was truncated t
o '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\xtree(162) : warning C4786: 'std::_Tree<employee,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::_Tree<employe
e,employee,std::multiset<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\utility(21) : warning C4786: 'std::pair<std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::iterato
r,bool>::pair<std::_Tree<employee,employee,std::set<employee,std::less<employee>,std::allocator<employee> >::_Kfn,std::less<employee>,std::allocator<employee> >::iterator,bool>' : identifier was truncated to '255' characters in the debug information
--------


Originally posted by Philip Nicoletti
you forgot an include :


#include <string>

Philip Nicoletti
February 20th, 2003, 08:47 AM
That is a VC++ thing. I think that they got rid of
these warnings in the latest version.

You can disable them by putting this at the top
of your code :


#pragma warning(disable:4786)

PaulWendt
February 20th, 2003, 10:47 AM
I just wanted to note that both of these questions have already
been answered to George2 in an older post. This is why I get
upset with the guy; he not only asks a superfluous number of
questions ... but he asks them over and over....

Here's a link to the older post to refresh your memory, George2:
http://www.codeguru.com/forum/showthread.php?s=&threadid=230206

It wasn't really that long ago, either.

--Paul

George2
February 20th, 2003, 09:00 PM
Thanks, Philip buddie!

George

Originally posted by Philip Nicoletti
That is a VC++ thing. I think that they got rid of
these warnings in the latest version.

You can disable them by putting this at the top
of your code :


#pragma warning(disable:4786)

George2
February 20th, 2003, 09:02 PM
I am really sorry, PaulWendt.

Next time I will try my best, trust me!


George

Originally posted by PaulWendt
I just wanted to note that both of these questions have already
been answered to George2 in an older post. This is why I get
upset with the guy; he not only asks a superfluous number of
questions ... but he asks them over and over....

Here's a link to the older post to refresh your memory, George2:
http://www.codeguru.com/forum/showthread.php?s=&threadid=230206

It wasn't really that long ago, either.

--Paul