Hi,
I am stumped as to how I can access a map container. I am reading the key into the map via file I/O and later on in my program I want to print the contents of the map using a class print method. My problem is I don't know how to access my specific map in the print method. I can't pass it to the class (that I know of) because the key is not global and I can't pass it by reference because the class doesn't know what my map is.
This is what I have so far
1 2 3 4 5 6 7 8 9 10
void SRC::PrintSRCInfo()
{
// print out Student Record
Records::iterator n;
cout << "Now the Record looks like:\n";
cout << "The ID is " << n->first << " ";
cout << "The Grade is " << n->second.TheGrade << " ";
cout << "The Name is " << n->second.TheName << endl;
}
Is map a member of this class?
Or should it be passed externally?
Why you cannot use following code:
1 2 3 4 5 6 7 8 9 10
void SRC::PrintSRCInfo(const Record& rmap)
{
// print out Student Record
cout << "Now the Record looks like:\n";
for(constauto& n: rmap) {
cout << "The ID is " << n.first << " ";
cout << "The Grade is " << n.second.TheGrade << " ";
cout << "The Name is " << n.second.TheName << endl;
}
}
I figured it out it was suppose to be in public and I was calling it incorrectly in my code.
I was doing this
Records SR;
instead of this
SRC::Records SR;
and the print method ended up looking like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// class declaration
void PrintSRCInfo(Records& rmap);
// class implementation
void SRC::PrintSRCInfo(Records& rmap)
{
SRC::Records::iterator n;
cout << "Now the Record looks like:\n";
for(n = rmap.begin(); n != rmap.end(); n++)
{
cout << "The ID is " << n->first << " ";
cout << "The Grade is " << n->second.TheGrade << " ";
cout << "The Name is " << n->second.TheName << endl;
}
}