Reputation: 540
I am creating a map in C++ which contains a integer key and the value is an object of class User. I am able to insert the object into the map with the following code -
std::map<std::string,User>::iterator it = usermap.begin();
usermap.insert (it, std::pair<string,User>(object.userid,object));
The code I am using to write the objects in a .bin file is -
map<std::string, User>::iterator it;
for ( it = usermap.begin(); it != usermap.end(); it++ )
{
myfile2 << "Object:" << it->second << "\n";
}
But the error I am getting when I am trying to run the code is -
In file included from /usr/include/c++/4.8.2/iostream:39:0, from a3part2_5.cpp:2: /usr/include/c++/4.8.2/ostream:548:5: note: template std::basic_ostream& std::operator<<(std::basic_ostream&, const unsigned char*) operator<<(basic_ostream& __out, const unsigned char* __s) ^ /usr/include/c++/4.8.2/ostream:548:5: note: template argument deduction/substitution failed: a3part2_5.cpp:90:31: note: cannot convert ‘it.std::_Rb_tree_iterator<_Tp>::operator->, User> >()->std::pair, User>::second’ (type ‘User’) to type ‘const unsigned char*’ myfile2 << "Obejct: " << it->second << "\n";
Any solutions for resolving the error?
My User class is defined as -
class User
{
public:
string userid; string uid; string gid; string gecos; string directory; string shell;
User() {}
};
Upvotes: 0
Views: 1621
Reputation: 809
An example:
ostream& operator << (ostream &os, User const& u){
os << "userid:\t" u.userid << "\n";
// ...
return os;
}
Upvotes: 1
Reputation: 2259
User
is user-defined class and hence ostream
cannot understand your User
objects when you do myfile2 << "Object:" << it->second << "\n";
This should help Overloading the << Operator for Your Own Classes.
So you should overload operator <<
for your class.
Upvotes: 4