[try Beta version]
Not logged in

 
using map with a class method

May 19, 2013 at 6:33am
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 May 19, 2013 at 6:35am
May 19, 2013 at 6:44am
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;
	}
}
May 19, 2013 at 7:00am
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).
May 19, 2013 at 7:16am
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.
May 19, 2013 at 6:41pm
where does the map container go in my class declaration?
May 19, 2013 at 6:52pm
Wherever you need it. If you need it, that it.
May 19, 2013 at 6:54pm
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
May 19, 2013 at 6:55pm
How do you use it?
May 19, 2013 at 6:59pm
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 );
}
May 19, 2013 at 7:02pm
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
May 19, 2013 at 7:04pm
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!!
May 19, 2013 at 7:24pm
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.