Fix basic error: no match for 'operator!=' in 'i != ...

This is a discussion on Fix basic error: no match for 'operator!=' in 'i != ... within the C++ Programming forums, part of the General Programming Boards category; Hello all at Cprogramming. I'm hoping someone can help me with this error. It probably involves changing one line... or ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Fix basic error: no match for 'operator!=' in 'i != ...

    Hello all at Cprogramming. I'm hoping someone can help me with this error. It probably involves changing one line... or I've made a stupid mistake or something. I've never seen the error before though.

    Anyways:

    Attempt to compile with gcc: C:\>gcc -o ywordf ywordf.cpp

    Error:
    ywordf.cpp: In function `int main(int, char**)':
    ywordf.cpp:30: error: no match for 'operator!=' in 'i != __gnu_norm::multimap<_Key, _Tp, _Compare, _Alloc>::rend() [with _Key = size_t, _Tp = std::string, _Compare = std::less<size_t>, _Alloc = std::allocator<std:air<const size_t, std::string> >]()'

    Code:
    Code:
    #include <set>
    #include <map>
    #include <fstream>
    #include <iostream>
    #include <iterator>
    #include <utility>
    
    using namespace std;
    
    int main(int ac, char** av)
    {
    if(ac != 2)
    {
    cout << "Usage: " << av[0] << " filename" << endl;
    return 1;
    }
    cout << "Reading file " << av[1] << endl;
    ifstream f(av[1]);
    
    // read and count words
    istream_iterator<string> i(f);
    multiset<string> s(i, istream_iterator<string>());
    
    // sort by count
    multimap<size_t, string> wordstats;
    for(multiset<string>::const_iterator i = s.begin(); i != s.end(); i = s.upper_bound(*i))
    wordstats.insert( make_pair( s.count(*i), *i ));
    
    // output in decreasing order
    for( multimap<size_t, string>::const_reverse_iterator i = wordstats.rbegin(); i != wordstats.rend(); ++i)
    cout << " word " << i->second << " found " << i->first << " times " << endl;
    }
    The error is in line 28, which is:
    Code:
    for( multimap<size_t, string>::const_reverse_iterator i = wordstats.rbegin(); i != wordstats.rend(); ++i)
    '

    What on earth am I doing wrong? What do I need to change in this line?

    I've never seen this error before, but I believe it has something to do with having or not having a const value in the for(multimap( paramaters.

    Help! and Thank You! I would be happy to post the purpose of my code, but its not necessary. People far more experienced than I can tell exactly what I'm doing.

    AA.

  2. #2
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    code formatting please
    -- Will you show me how to c++?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So for me, changing const_reverse_iterator to just plain reverse_iterator caused the error to go away. Not sure why, exactly.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I have no problem compiling this code on Ubuntu with g++ 4.4.1

    looks like problem with your stl version
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by mramazing View Post
    code formatting please
    Not really sure what you mean...

    I use GCC via Cygwin, as well as Visual Studio 2010 to compile my C++. Does that help?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    This topic is now resolved.

    The code that I posted above can be successfully compiled with g++, via Cygwin on a Windows system. (Thanks for the heads up, vart).

    It returns errors with or without const, on Visual Studio, as well as GCC.

    Thank you for all your help, vart,tabstop, mramazing.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,312
    Looking at the error message, my guess is that because wordstats is non-const, wordstats.rend() returns a reverse iterator to non-const. Now, perhaps the relevant operator!= is a function template with just one template parameter, so the compiler cannot find a matching operator!= since the types of i and the return value of wordstats.rend() are different.

    One way out is:
    Code:
    // output in decreasing order
    for( multimap<size_t, string>::const_reverse_iterator i = wordstats.rbegin(), end = wordstats.rend(); i != end; ++i)
        cout << " word " << i->second << " found " << i->first << " times " << endl;
    which also provides the benefit of not evaluating rend() on each iteration. However, MSVC9 still appears to choke, but on the printing of a const std::string. So, for the time being, it is still better to just use a reverse iterator to non-const (possibly with the caching of rend()).

    Quote Originally Posted by aeolusaether
    Not really sure what you mean...
    Read this article on indentation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by laserlight View Post
    Looking at the error message, my guess is that because wordstats is non-const, wordstats.rend() returns a reverse iterator to non-const. Now, perhaps the relevant operator!= is a function template with just one template parameter, so the compiler cannot find a matching operator!= since the types of i and the return value of wordstats.rend() are different.
    I didn't see it.

    We need a new warning. "This would have worked except for a const mismatch."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 05:28 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21