Compile-time error dereferencing Map iterator

This is throwing a compile-time error: 'class string' has no member named 'first' (or 'second') for every instance of dereferencing the iterator. Below is a fragment of the function that is causing the error. Any ideas?

You can see the full code here: https://pastebin.com/47Fi12Xy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Map<string, int>::iterator voteCounter = countMap.begin();
for (; voteCounter != countMap.end(); ++voteCounter) {
   int districtsWon = 0;
   Map<string, string>::iterator partyCounter = districtWinner.begin();
   for (; partyCounter != districtWinner.end(); ++partyCounter) {
      if (voteCounter->first == partyCounter->second)
         ++districtsWon;
   }
   double percentOfVotes = voteCounter->second / totalVotes;
   double percentDistrictsWon = districtsWon / (double) districtCounter; // 
   districts won by party / # of districts
   double ratio = percentDistrictsWon / percentOfVotes;
   gerryMap[voteCounter->first] = ratio;
   }
   return gerryMap;
}
Last edited on
If Map is basically a std::map, then I don't see the problem.
I believe the site uses Stanford because when I change Map to std::map it throws this error: "Your code is trying to combine two incompatible data types 'map' and 'Map'. conversion from 'map<string, double>' to non-scalar type 'Map<string, double>' requested"
What exactly is a Map?

What line is causing the error?

Have you considered range based loops for those two for loops since that is one of the purposes of range based loops is to make iterating over a container easier?

Errors occur at lines 6, 9, and 13 where it is dereferences the iterator. A range based loop returns the same errors. This code works fine (with minor changes) in Visual C++ but I'm trying to refactor it to work on the original website: https://www.codestepbystep.com/problem/view/cpp/collections/map/gerrymanderingRatios
Last edited on
I don't know what a Stanford Map is, or a Map<>::iterator, so I can't help you.

The code looks like it should work with a std::map, but if you want to change it to std::map, you would need to change it everywhere, not just in some places.

And your pastebin link doesn't show full code since there are no headers and no main.
Last edited on
Ok, thanks. There are no main/headers in my pastebin because of the web site I'm using requires you to just enter the function (the URL is in my post above) and it tests your code using the websites backend.

The website says to use Stanford or STL collections, but obviously std::map doesn't work. Here is a link to the Stanford Map(): http://stanford.edu/~stepp/cppdoc/Map-class.html

Incidentally, the code compiles and runs in Visual C++.
How can it "compile and run" in Visual C++ without headers and a main and using Map, which obviously doesn't exist in Visual C++?
Because in Visual C++ I include the headers, function prototypes, and of course a main(). I also use std::map. The only difference between std::map and Map (in my example anyway) are couple functions (i.e. Map doesn't use at, insert, or insert_or_assign.)

My VC++ code: https://pastebin.com/S1XXC9ya

Data I used to test: https://pastebin.com/TnipswPR

I think this whole issue is related to Stanford's C++ library. Everyone says don't use it if you don't have to. Unfortunately, in this case I think I have to because of that web site I'm using.
Last edited on
The site you posted (http://stanford.edu/~stepp/cppdoc/Map-class.html) doesn't indicate that Map has a type named Map::iterator. So it seems you're using an unsupported interface or something.
Stanford's documentation is terrible; however, looking at the source code it appears that their iterator only returns a key, not a key:value:

https://github.com/stepp/stanford-cpp-library/blob/ce010b711cdf8ad505ba82b46dc5bda1c6571648/StanfordCPPLib/collections/map.h#L1059
Topic archived. No new replies allowed.