473,796 Members | 2,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compilation error with const_reverse_i terator

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_ite rator i = si.begin(); i != si.end(); +
+i)
is.insert(make_ pair(i->second, i->first));

for (multimap<int, string>::const_ reverse_iterato r 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_iterato r 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 2123
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_ite rator i = si.begin(); i != si.end(); +
+i)
is.insert(make_ pair(i->second, i->first));

for (multimap<int, string>::const_ reverse_iterato r 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_iterato r r = is.rbegin(); r !
= is.rend(); ++r)
Ah,
As you declare
>multimap<int , stringis;
then /is.rend()/ and /is.rbegin()/ are both reverse_iterato rs
r = is.begin(), compiles as is convertible from reverse_iterato r to
const_reverse_i terator.

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

so you can simply choose reverse_iterato r 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_ite rator i = si.begin(); i != si.end(); +
+i)
is.insert(make_ pair(i->second, i->first));

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

const_reverse_i terate != reverse_iterato r

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

return 0;
}

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

for (multimap<int, string>::const_ reverse_iterato r 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<>::rev erse_iterator is defined to be

std::reverse_it erator< std::map<>::ite rator >

and std::map<>::con st_reverse_iter ator is defined to be

std::reverse_it erator< std::map<>::con st_iterator >

The standard only requires that std::reverse_it erator supports comparison
for reverse_iterato rs 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
2363
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 begun to use GNU Autotools. I'm sorry to say I'm new to gcc as well :( Now I get the most ridiculous compile error which I'm unable to solve. Can someone, please, help me with this? gcc output together with the files mentioned in the gcc error...
6
2226
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. -------------------------------------------------------------------------------- Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details
0
1477
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 at this point. Please help Cheer
3
3354
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 from 'c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\root\1f575646\ad3a161b\assembly\dl2\57ca505e\044565c0_f84fc401\Test1.DLL' The problem is that the control is defined in two different assemblies
6
2908
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 error. For example, broken.aspx might only contain the following line of code: <% Dim x as %> When I visit this page from my Web browser, I see all the helpful ASP.NET Server Error information, including:
2
1583
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 insights would be greatly appreciated. tia, Slawek
20
3586
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 reverse_iterator but not const_reverse_iterator: for(vector<int>::const_reverse_iterator i = v.rbegin(); i != v.rend(); ++i) cout << (*i) << endl;
6
1480
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) { // Convert the int to a string. std::string numString = boost::lexical_cast<std::string>(number); std::string::const_reverse_iterator i = numString.rbegin(), end =
1
2812
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 incorporate the same logic within my simulation, the class that implements the functor logic has problems compiling. I get the following errors: -- Building myTest.cpp --
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.