Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is probably a silly error but i cant seem to find what i have done wrong.

The error i am getting is 'no operator "=" matches these opperands'.

Here is my code...

C++
void print_words(const map < string, int >& m1) {
    map<string, int>::iterator it;
    cout << "Number of non-empty words: " << m1.size() << '\n';
    
    int count = 0;
    for (it = m1.begin(); it != m1.end(); it++) {
    
    }
}

I get the error in the for loop in the it = m1.begin() statement and i cannot go on to print out the map if i cant iterate through it so any help would be much appreciated.

What I have tried:

I tried finding any simple typos or errors but cannot find any. I set up an iterator in another method and it worked completely fine so i am really stumped on why i am getting this error.
Posted
Updated 30-Sep-17 21:19pm
v2
Comments
Richard MacCutchan 1-Oct-17 3:17am    
What error?

1 solution

Just use auto keyword to define your variable type. C++ compiler will infer the correct type for you.

C++
void print_words(const std::map < std::string, int >& m1) {
	std::cout << "Number of non-empty words: " << m1.size() << '\n';

	int count = 0;
	for (auto it = m1.begin(); it != m1.end(); ++it) {

	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900