Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - compilation error with const_reverse_iterator

 
Thread Tools Search this Thread
Old 10-04-2007, 07:12 AM   #1
Default 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, int> si;

string word;

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

multimap<int, string> is;

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:air<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



subramanian100in@yahoo.com, India
  Reply With Quote
Old 10-04-2007, 07:23 AM   #2
Barry
 
Posts: n/a
Default Re: compilation error with const_reverse_iterator
, India wrote:
> Consider the following program:
>
> #include <iostream>
> #include <map>
> #include <string>
> #include <utility>
> #include <algorithm>
>
> using namespace std;
>
> int main()
> {
> map<string, int> si;
>
> string word;
>
> while (cin >> word)
> ++si[word];
>
> multimap<int, string> is;
>
> 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, string> is;

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:air<const
> int, std::string> >]()'
>
> However this program compiles fine under VC++ 2005 Express Edition.
>


I think this is an extension from VC.


Barry
  Reply With Quote
Old 10-04-2007, 07:40 AM   #3
Kai-Uwe Bux
 
Posts: n/a
Default Re: compilation error with const_reverse_iterator
, India wrote:

> Consider the following program:
>
> #include <iostream>
> #include <map>
> #include <string>
> #include <utility>
> #include <algorithm>
>
> using namespace std;
>
> int main()
> {
> map<string, int> si;
>
> string word;
>
> while (cin >> word)
> ++si[word];
>
> multimap<int, string> is;


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:air<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


Kai-Uwe Bux
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT Help needed with ASP.NET Compilation error nunu_bug General Help Related Topics 0 08-06-2009 01:26 PM
Can anybody explain me of c compilation? sajeev Software 0 08-01-2008 11:51 AM
Making a Music DVD compilation from my DVD's astraea41@aol.com DVD Video 0 12-05-2006 03:37 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46