Compile-time error dereferencing Map iterator

Feb 15, 2019 at 10:37pm
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 Feb 15, 2019 at 11:42pm
Feb 15, 2019 at 10:52pm
If Map is basically a std::map, then I don't see the problem.
Feb 15, 2019 at 10:57pm
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"
Feb 15, 2019 at 10:57pm
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?

Feb 15, 2019 at 11:41pm
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 Feb 15, 2019 at 11:42pm
Feb 16, 2019 at 12:22am
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 Feb 16, 2019 at 12:24am
Feb 16, 2019 at 1:06am
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++.
Feb 16, 2019 at 1:56am
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++?
Feb 16, 2019 at 3:05am
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 Feb 16, 2019 at 3:11am
Feb 16, 2019 at 4:23pm
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.
Feb 16, 2019 at 5:09pm
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.