Passing a <map> by reference to a function.

Hey cpluplus community! I'm having a little trouble figuring out how to pass a map by reference to a function.

This is the function that I have created.

1
2
3
4
5
6
7
8
9
10
11
12
 bool searchMap(string _token, map<string, Token*>& myMap)
{
	for (map<string, Token*>::iterator map = myMap.begin(); map != myMap.end(); ++map) // Checking if the keyword is already in the map
	{
		if (map->first == _token)
		{
			map->second->plusCount(); //increment count for evertime keyword is found
			return true;
		}
	}
	return false;
}


And this is how I'm trying to use it in my program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

if (isalpha(charBuf))
{
        numBuf.clear(); // Clearing Num Buffer because there is no <digit><character> in syntax
	strBuf += charBuf;
	if (searchMap(strBuf, tokenMap&))
	{
		strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
	}
	else if (keywordSearch(strBuf, keyword))
	{
		Token * newToken = new Token("Keyword", 1); // DONT FORGET TO DELETE!
		tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token
		strBuf.clear(); // Clear strBuf to make room for next token.
	}
}

The searchMap function call on line 6 is where the error is. Error is showing right at the first ')' after the '&'.
ERROR: Expected an expression.
I'm guessing there is something wrong with my syntax but I can't figure it out. Is there another way I should be passing the <map> to a function?
Any help will be great, thanks!
Last edited on
if (searchMap(strBuf, tokenMap&))
You don't need the & when passing by reference. Simply pass it as you would to any other function.
Is the & in the function definition needed?
Is the & in the function definition needed?

Yes, because that tells C++ you want to pass by reference. When you're calling the function, you don't need it. It's just the syntax.
I removed the & in the function call and I get tokenMap as an error.

This is the map that I have declared as a global variable.
 
map<string, Token*>tokenMap;
I removed the & in the function call and I get tokenMap as an error.

What error do you get? Be more specific.

This is the map that I have declared as a global variable.

If it's a global variable, why do you need to pass it as an argument? Simply use it directly.
This is the error without the &
1
2
3
'bool searchMap(std::string,std::map<std::string,Token,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &)': 
cannot convert argument 2 from 'std::map<std::string,Token *,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' 
to 'std::map<std::string,Token,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &'		
I'm not using it directly because the function makes the program easier to read and more organized. I have to search through the map multiple times throughout the program.
Last edited on
Your error message:
cannot convert argument 2 from
std::map<std::string,Token*, /*...*/> to
std::map<std::string,Token , /*...*/> &


The ellipsized bit is equivalent -- tells you about the comparator and the allocator, which are both default.

The implication is that tokenMap is not what you think it is.
Please post more of your code. More context is needed, and I expect you got more than one error.
This is my main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "Token.h"

using namespace std;

int main()
{
	ifstream inFile;
	map<string, Token*>tokenMap;

	cout << "Please enter the file name to by read: ";
	string readFile;
	getline(cin, readFile);
	inFile.open(readFile);
	if (inFile.is_open())
	{
		char charBuf; // To hold each character read by inFile stream
		string strBuf; // To collect past characters read
		string numBuf; // To collect past numbers read
		array<string,5> keyword = {"if", "then", "else", "begin", "end"};
		array<char,9> special = { '(', ')','[',']','+','-','=',',',';' };
		while (inFile.get(charBuf))
		{
			//****** What if inFile reads a delimiter i.e \n \t or space******
			if (charBuf == ' ' || charBuf == '\n' || charBuf == '\t')
			{
				if (strBuf.length > 0)
				{
					if (searchMap(strBuf, tokenMap))
					{
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else if (keywordSearch(strBuf, keyword))
					{
						Token * newToken = new Token("Keyword", 1); // DONT FORGET TO DELETE!
						tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else
					{
						Token * newToken = new Token("Identifier", 1); // DONT FORGET TO DELETE!
						tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
				}
				else if (numBuf.length() > 0)
				{
					if (searchMap(numBuf, tokenMap&))
					{
						numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else
					{
						if (numBuf.find("."))
						{
							Token * newToken = new Token("Real", 1); // DONT FORGET TO DELETE!
							tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
							numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
						}
						else
						{
							Token * newToken = new Token("Integer", 1); // DONT FORGET TO DELETE!
							tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
							numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
						}
					}
				}
				break;
			}
			//******CASE IF NO DELIMITER IS READ, CHECK IF ITS AN ALFHA******
			if (isalpha(charBuf))
			{
				numBuf.clear(); // Clearing Num Buffer because there is no <digit><character> in syntax
				strBuf += charBuf;
				if (searchMap(strBuf, tokenMap&))
				{
					strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
				}
				else if (keywordSearch(strBuf, keyword))
				{
					Token * newToken = new Token("Keyword", 1); // DONT FORGET TO DELETE!
					tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token
					strBuf.clear(); // Clear strBuf to make room for next token.
				}
			}
			else if (charBuf == '.')
			{
				numBuf += charBuf;
			}
			else if (isdigit(charBuf))
			{
				strBuf.clear(); // Clearing String Buffer because there is no <digit><character> in syntax
				numBuf += charBuf;
				if (searchMap(numBuf, tokenMap&))
				{
					numBuf.clear(); // If a keyword was found clear strBuf to make room for next token
				}
			}
			//****** The char is not alpha or digit, last case is special ******
			if (specialSearch(charBuf, special))
			{
				if(strBuf.length > 0)
				{ 
					if (searchMap(strBuf, tokenMap&))
					{
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else if (keywordSearch(strBuf, keyword))
					{
						Token * newToken = new Token("Keyword", 1); // DONT FORGET TO DELETE!
						tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else
					{ 
						Token * newToken = new Token("Identifier", 1); // DONT FORGET TO DELETE!
						tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
				}
				else if (numBuf.length() > 0)
				{
					if (searchMap(numBuf, tokenMap&))
					{
						strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
					}
					else
					{
						if (numBuf.find("."))
						{
							if (numBuf.find(".") == numBuf[numBuf.length()])
							{
								numBuf = numBuf.substr(0, numBuf.length() - 1);
								Token * newToken = new Token("Integer", 1); // DONT FORGET TO DELETE!
								tokenMap.insert(pair<string, Token*>(numBuf, newToken)); // Not found in the map so create a new token and store pointer in map
								numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
							}
							else
							{
								Token * newToken = new Token("Real", 1); // DONT FORGET TO DELETE!
								tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
								numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
							}
						}
						else
						{
							Token * newToken = new Token("Integer", 1); // DONT FORGET TO DELETE!
							tokenMap.insert(pair<string, Token*>(numBuf, newToken)); // Not found in the map so create a new token and store pointer in map
							numBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
						}
					}
				}
				strBuf = charBuf; // Set strBuf to the special character to search through the map
				if (searchMap(strBuf, tokenMap&))
				{
					strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
				}
				else
				{
					Token * newToken = new Token("Special", 1); // DONT FORGET TO DELETE!
					tokenMap.insert(pair<string, Token*>(strBuf, newToken)); // Not found in the map so create a new token and store pointer in map
					strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
				}
			}
		}

	}
	else
	{
		cout << "Something went wrong and the file was not opened.";
	}
	return 0;


I'm thinking that it might have something to do with the pointer to Token in the <map>. I may have to rethink the way I create the Token objects. There is an error for every call the searchMap object. I know that there are a few error, quit possibly logic errors but I haven't dived into the debugging yet. I'm just trying to figure out how to write this function and use it.
Last edited on
Your code needs to be reworked. 172 lines and 8 levels of nesting makes for a large and unwieldy block of code. Try splitting each section into independent functions.
I added these lines to the top of the code you posted as a mock because you didn't provide the real definitions.

1
2
3
4
5
6
7
8
9
10
11
# include <fstream>
# include <iostream>
# include <array>
# include <map>
# include <vector>
using namespace std;

struct Token { Token(string, int) {}; };
bool keywordSearch(string, std::array<string, 5>) { return true; }
bool specialSearch(char, std::array<char, 9ul>) { return true; }
bool searchMap(string, map<string, Token*>) { return true; }  


I think you'll find it helpful and quicker to write code if you don't wait until your program is complete to try to compile it. Develop your program in tiny steps, and refine your program as you go.
Fix errors as they appear; your code should almost always be ready to compile and run. Consider using source control to help you.

I was able to get it to compile. I didn't do much more than fix the errors, though.
There was a missing paren. Perhaps that had something to do with your error:

http://coliru.stacked-crooked.com/a/1fe7d69255cf5a86

Last edited on
Topic archived. No new replies allowed.