I'm trying to output the contents of my map but i'm not sure on how to go about it. i've got the following declarations for a map and a iterator set as private in my header file
typedef map<long, string> Student;
Student newstudent;
typedef Student::const_iterator studentItr;
studentItr itr;
and i have the following declaration in my .cpp
map<long, string> classname::GetMap()
{
for(itr = newstudent.begin(); itr !=newstudent.end(); itr++)
{
cout << itr->first << " => " <<itr->second << '\n';
}
return newstudent;
}
and this is how i'm trying to pass this into my overloaded operator << function
ostream & operator <<( ostream & os, classname & R)
{
os << " " << R.GetMap() << '\n';
return os;
}
Usually this would work for any other function but since its a map i get the following error:
no match for 'operator<<' (operand types are 'std::basic_ostream< char >' and 'std::map < long int, std::__cxx11::basic_string< char > > ')|
Could anybody point me in the right direction of what i'm doing wrong
EDIT: Thanks for posting your suggestions i appreciate the help, but i think something isnt right either with my complier or with how i've stated something.
Everytime i try using Student as a type it wont register as a type and i get an error about it.
E.g if i declare this statement
ostream & operator <<( ostream & os, const Student & R)
OR
Student & classname::GetMap()
i get the error:
'Student' does not name a type
This error ultimately is leading to nothing within my statements making any sense and i'm not sure how to fix that
So you seem to have put the output code inside for GetMap function for some reason. Either you want a routine called GetMap which just gets a map, or you want a routine called PrintMap (for instance) which outputs the map. You have half and half. Here's one way to do it
void classname::PrintMap() const
{
for(itr = newstudent.begin(); itr !=newstudent.end(); itr++)
{
cout << itr->first << " => " <<itr->second << '\n';
}
}
ostream & operator <<( ostream & os, classname & R)
{
os << " ";
R.printMap();
os << '\n';
return os;
}
This could be improved, for instance you could pass the stream you want to output to, to the PrintMap function. Like this
void classname::PrintMap(ostream & os) const
{
for(itr = newstudent.begin(); itr !=newstudent.end(); itr++)
{
os << itr->first << " => " <<itr->second << '\n';
}
}
ostream & operator <<( ostream & os, classname & R)
{
os << " ";
R.printMap(os);
os << '\n';
return os;
}
FInally you could follow the common convention and rename PrintMap as operator<< and make it a friend function instead of a member function.
Student& classname::GetMap()
{
return newstudent;
}
ostream & operator<<(ostream & os, const Student& s)
{
for(itr = s.begin(); itr !=s.end(); itr++)
{
os << itr->first << " => " <<itr->second << '\n';
}
return os;
}
ostream & operator <<( ostream & os, classname & R)
{
os << " " << R.getMap() << '\n';
}
You could make an operator << for your map type. (totally untested code)
ostream & operator <<( ostream & os, const Student & R) {
for(studentIter itr = R.begin(); itr !=R.end(); itr++){
os << itr->first << " => " <<itr->second << '\n';
}
return os;
}
Edit: Changed the names to match those in your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With