using map with a class method

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;
}


*edit Records is a typedef for my map
 
typedef map<int,SRC> Records;
Last edited on
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(const auto& n: rmap) {
		cout << "The ID is " << n.first << " ";
		cout << "The Grade is " << n.second.TheGrade << " ";
		cout << "The Name is " << n.second.TheName << endl;
	}
}
I don't think that works because map is using the SRC class right? I tried to put it in private but it didn't like map there(without the typedef).
I don't think that works because map is using the SRC class right?
Class members can accept its class and containers of said class as arguments.
Just try it.
where does the map container go in my class declaration?
Wherever you need it. If you need it, that it.
I've put it above public, in public and in private and the print method will not accept it. I get an error C2061 identifier Records
How do you use it?
http://www.cplusplus.com/forum/articles/40071/#msg216270

¿how is your code different from this?
1
2
3
4
5
6
7
8
void print_map(std::map<int, std::string> &m){

}

int main(){
   std::map<int, std::string> a_map;
   print_map( a_map );
}
Fun thing that error C2061 identifier Records is probably full error message.
http://cboard.cprogramming.com/cplusplus-programming/117317-error-c2061-syntax-error-identifier.html
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;
	}
}


thanks for all your help!!
1
2
// class declaration
void PrintSRCInfo(Records& rmap);
if you are not using the state of the caller object, then it shouldn't be a non-static member function.

@MiiNiPaa: quite funny, indeed.
Topic archived. No new replies allowed.