473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

compilation error with const_reverse_iterator

Consider the following program:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>

using namespace std;

int main()
{
map<string, intsi;

string word;

while (cin >word)
++si[word];

multimap<int, stringis;

for (map<string, int>::const_iterator i = si.begin(); i != si.end(); +
+i)
is.insert(make_pair(i->second, i->first));

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)
cout << r->second << " " << r->first << endl;

return 0;
}

Under g++, I get compilation error for the line

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)

The actual error message is

error: no match for 'operator!=' in 'r != std::multimap<_Key, _Tp,
_Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
_Compare = std::less<int>, _Alloc = std::allocator<std::pair<const
int, std::string]()'

However this program compiles fine under VC++ 2005 Express Edition.

I use the following compilation command under g++.

g++ -std=c++98 -pedantic -Wall -Wextra word_count.cpp

Kindly explain why I am getting error for the above mentioned line
under g++ only.

Thanks
V.Subramanian

Oct 4 '07 #1
2 2097
su**************@yahoo.com, India wrote:
Consider the following program:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>

using namespace std;

int main()
{
map<string, intsi;

string word;

while (cin >word)
++si[word];

multimap<int, stringis;

for (map<string, int>::const_iterator i = si.begin(); i != si.end(); +
+i)
is.insert(make_pair(i->second, i->first));

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)
cout << r->second << " " << r->first << endl;

return 0;
}

Under g++, I get compilation error for the line

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)
Ah,
As you declare
>multimap<int, stringis;
then /is.rend()/ and /is.rbegin()/ are both reverse_iterators
r = is.begin(), compiles as is convertible from reverse_iterator to
const_reverse_iterator.

Threre are no /operator !=/ or /operator ==/ between the two types.

so you can simply choose reverse_iterator for r
>
The actual error message is

error: no match for 'operator!=' in 'r != std::multimap<_Key, _Tp,
_Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
_Compare = std::less<int>, _Alloc = std::allocator<std::pair<const
int, std::string]()'

However this program compiles fine under VC++ 2005 Express Edition.
I think this is an extension from VC.
Oct 4 '07 #2
su**************@yahoo.com, India wrote:
Consider the following program:

#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>

using namespace std;

int main()
{
map<string, intsi;

string word;

while (cin >word)
++si[word];

multimap<int, stringis;
Note that this is a non-const object.
>
for (map<string, int>::const_iterator i = si.begin(); i != si.end(); +
+i)
is.insert(make_pair(i->second, i->first));

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)
Note that is.rend() returns reverse_iterator and not const_reverse_iterator
since "is" is a non-const object. Thus, you compare

const_reverse_iterate != reverse_iterator

cout << r->second << " " << r->first << endl;

return 0;
}

Under g++, I get compilation error for the line

for (multimap<int, string>::const_reverse_iterator r = is.rbegin(); r !
= is.rend(); ++r)

The actual error message is

error: no match for 'operator!=' in 'r != std::multimap<_Key, _Tp,
_Compare, _Alloc>::rend() [with _Key = int, _Tp = std::string,
_Compare = std::less<int>, _Alloc = std::allocator<std::pair<const
int, std::string]()'
Yup. That's what you get.

However this program compiles fine under VC++ 2005 Express Edition.

I use the following compilation command under g++.

g++ -std=c++98 -pedantic -Wall -Wextra word_count.cpp
It's a matter which STL implementation you use.
Kindly explain why I am getting error for the above mentioned line
under g++ only.
You hit upon a defect in the language.

std::map<>::reverse_iterator is defined to be

std::reverse_iterator< std::map<>::iterator >

and std::map<>::const_reverse_iterator is defined to be

std::reverse_iterator< std::map<>::const_iterator >

The standard only requires that std::reverse_iterator supports comparison
for reverse_iterators with identical underlying iterator types. Thus, g++
is formally correct.

HOWEVER, this has been fixed in the draft for the next revision of the C++
standard. It also has been fixed in g++. Your code compiles fine with
g++-4.1.1.
Best

Kai-Uwe Bux
Oct 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
6
by: Joachim | last post by:
I made some project changes (which seems it doesn't help if I undo) which have created compilation error: " Server Error in '/PCSWebApp1' Application....
0
by: James Zhuo | last post by:
hi all I changed the name of the class LoginPage to a different name "LoginPageOne" But the same error gets generated with the Wiliam.Request.LoginPageOne. That pretty much leaves me clueless...
3
by: Dan | last post by:
Hi, I have a problem using an aspx page with a Control on it. I get the following error message Compiler Error Message: CS1595: 'Test.Class2' is defined in multiple places; using definition...
6
by: Plat | last post by:
I've Googled this for a while, to no avail. Hopefully someone can help me. Maybe I'm using the wrong terminology. Here's the scoop! Let's say I've got a simple *.ASPX page that has a syntax...
2
by: Serengeti | last post by:
hello, I've had some hard time trying to understand why does this code compile: http://cpp.sourceforge.net/?show=10151 (*) while this doesn't: http://cpp.sourceforge.net/?show=10150 (**) Any...
20
by: Jess | last post by:
Hello, I think the two reverse_iterators are the same, except that the const_ version doesn't allow me to change the value pointed to by the iterators. However, I have a program that works for...
6
by: Michael Coley | last post by:
Hi, I've wrote this function which should add a comma for every 3 digits in a number (so that it looks something like 5,000). This is my function: std::string formatNumber(int number) { //...
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.