G++ error

This is a discussion on G++ error within the C++ Programming forums, part of the General Programming Boards category; Hi. I am trying to run this program. Code: #include <iostream> #include <string> #include <vector> #include <fstream> #include <algorithm> using ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    G++ error

    Hi. I am trying to run this program.
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <fstream>
    #include <algorithm>
    using namespace std;
    string make_plural(int count,const string &s1,const string &s2)
    {
        if(count<1)
        return s1;
        else
        return s1+s2;
    }
    bool have5(const string &s)
    {
        return s.size()>=5;
    }
    bool isShorter(const string &s1,const string &s2)
    {
        return s1.size()>s2.size();
    }
    void elimDuops(vector<string> &words)
    {
    sort(words.begin(),words.end());
    vector<string>::iterator it=unique(words.begin(),words.end());
    for(int i=0;i<words.size();++i)
    cout<<words[i]<<" ";
    cout << endl;
    words.erase(it,words.end());
    }
    
    void biggies(vector<string> &words,vector<string>::size_type sz)
    {
    
        elimDuops(words);
        stable_sort(words.begin(),words.end(),isShorter);
        vector<string>::iterator wc=find_if(words.begin(),words.end(),[sz](const string &a){return a.size()>=sz;});
    
    int count = words.end()-wc;
    cout << count << " " << make_plural(count, "word", "s" ) << " of length " << sz << " or longer" << endl;
    
    
    
    
    }
    int main(int argc,char **argv)
    {
        vector<string> fox;
        ifstream in(argv[1]);
        string s;
        while(in>>s&&!in.eof())
        fox[.push_back(s);
        for(int i=0;i<fox.size();++i)
        cout<<fox[i]<<" ";
        cout<<endl;
        partition(fox.begin(),fox.end(),have5);
        for(int i=0;i<fox.size();++i)
        cout<<fox[i]<<" ";
        cout<<endl;
        biggies(fox,5);
    
    }
    in codeblocks it runs fine. But when I run it in ubuntu terminal in g++ it gives the following error.

    10.13.cpp:37:110: error: no matching function for call to ‘find_if(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, biggies(std::vector<std::basic_string<char> >&, std::vector<std::basic_string<char> >::size_type)::<lambda(const string&)>)’
    10.13.cpp:37:110: note: candidate is:
    /usr/include/c++/4.6/bits/stl_algo.h:4418:5: note: template<class _IIter, class _Predicate> _IIter std::find_if(_IIter, _IIter, _Predicate)
    why is that?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Clearly you didn't even take the trouble to read the warnings g++ emitted immediately before that error message. If you had, you would have seen that g++ explained the reason for the error.

    g++ is not configured by default to support C++11. The offending line uses a lambda, which is C++-11 specific. Read the documentation for the necessary command line options to compile with C++11 support (at least, as far as g++ supports C++11).


    Also, line 52 will not compile with any version of C++, nor with any compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    I read that warnings..but there it was said to be enabled by default. Btw, line 52 is a typing mistake..

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    g++ is configured by default to emit that warning message, not configured to support C++-11.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Regarding flags: for starters, try -std=c++11. That should enable lambdas.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dwks View Post
    Regarding flags: for starters, try -std=c++11. That should enable lambdas.
    If your ubuntu still has g++ 4.6.3 then the flag would be -std=c++0x
    Kurt

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Good point. Error messages are coming from "/usr/include/c++/4.6/" in the original post. Didn't notice.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-29-2012, 04:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 10:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 10:12 PM

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