Hey guys I am trying to print the contents of a map container I have. I seem to be getting a nasty error from the compiler which seems to come from the pos = wordList.begin() part. any suggestions?

map<string, unsigned>::iterator pos;
     for(pos = wordList.begin(); pos != wordList.end(); ++pos)
         {
         cout << "Key: " << pos->first << endl;
         cout << "Value:" << pos->second << endl;
         }

some of the error:
error: no match for âoperator=â in âpos =

Dani AI

Generated

The compiler error is coming from a type mismatch: print_words takes a const map reference, so wordList.begin() yields a const_iterator. Declaring a non-const iterator and then assigning wordList.begin() to it produces the "no match for operator=" message. is correct — either iterate with a const_iterator, make the function accept a non-const map if you intend to change it, or let the compiler pick the right type (C++11 auto or a range-based for) so you avoid the mismatch entirely.

There are other bugs that will cause runtime trouble even after the iterator issue is fixed. remove_punctuation calls c_str() needlessly, indexes into fixWord without making space (writing fixWord[fixCount] on an empty string is undefined), and loops using word[i] != NULL instead of i < word.size() (or a range-based loop). Also use <cctype> and pass unsigned char to isalpha/tolower to avoid undefined behavior on negative char values.

A safe, modern approach (C++11+) — remove punctuation by appending lower-case letters, and iterate without naming iterator types explicitly:

std::string remove_punctuation(const std::string& s) {
    std::string out;
    out.reserve(s.size());
    for (char ch : s) {
        unsigned char uch = static_cast<unsigned char>(ch);
        if (std::isalpha(uch))
            out.push_back(static_cast<char>(std::tolower(uch)));
    }
    return out;
}
for (const auto& kv : wordList) {
    std::cout << "Key: " << kv.first << '\n'
              << "Value: " << kv.second << '\n';
}

Practical notes: include <cctype>, prefer '\n' over std::endl for performance unless flushing is needed, compile with warnings enabled (e.g., -Wall) to catch issues, and avoid system("pause") for portable code.

Recommended Answers

All 3 Replies

I assume it's talking about the assignment of pos=wordlist.begin() . If so you'd imagine that wordlist isn't of type map<string,unsigned> from what the error says.

Can you post more code so we can see more of what you're doing?

#include <iostream>
#include <map>
#include <sstream>
#include <fstream>
#include <cstring>

using namespace std;

unsigned int read_words(map <string, unsigned>&);
string remove_punctuation(const string&);
void print_words(const map <string, unsigned>&, unsigned int);

int main()
        {
        map<string, unsigned> wordList;

	unsigned int numwords;
	
	numwords = read_words(wordList);
	
	print_words(wordList, numwords);

    system ("pause");
	return 0;
	}

unsigned int read_words(map <string, unsigned>& wordList)
	{
    ifstream inFile;
	inFile.open("data5.txt");

	if(inFile.fail())
		{
		cout << "input file did not open";
		exit(0);
		}

	string word;
	string newWord;
	unsigned int numCount = 0;

	while(inFile >> word)
	{
	newWord = remove_punctuation(word);
	if(newWord.length() > 0)
		{
		wordList[newWord]++;
		numCount++;
	    }
    }
	return numCount;
	}

string remove_punctuation(const string& word)
       {       
       word.c_str();
       string fixWord;
       fixWord.c_str();
       int fixCount = 0;
       string temp;
       
       for(int i = 0; word[i] != NULL; ++i)
               {
               if(isalpha(word[i]))
               {
               fixWord[fixCount] = tolower(word[i]);
               ++fixCount;
               }
               }
       temp = fixWord;
       return temp;
       }

void print_words(const map <string, unsigned>& wordList, unsigned int numwords)
     {
     cout << numwords;
     
     map<string, unsigned>::iterator pos;
     for(pos = wordList.begin(); pos != wordList.end(); ++pos)
	 {
	 cout << "Key: " << pos->first << endl;
	 //cout << "Value:" << pos->second << endl;
	 }
     }

You're passing in a const map, so you've gotta use a const_iterator: map<string,unsigned>::const_iterator pos; should work.

To explain. A const_iterator doesn't allow you modify anything in the map, while you may with iterator s.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.