RyanWolfe9013
RyanWolfe9013

Reputation: 61

no match for "operator<<" (operand types are 'std::ostream'

In this program, I get the following error:

no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and '__gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >')

referring to the cout << count in the following code. I'm pretty new to c++ and unsure how to fix this. I looked over other examples similar to my error and none looked relational to mine. How do I fix this error? I'm guessing it has something to do with vector::size_type and vector.end() type auto conversion to count and not being able to convert to char or string, but how do I address this?

#include <algorithm>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

ostream &print(ostream &os, const vector<string> &vs)
{
    for(const auto &i: vs)
      os << i << " ";
    return os;
}

string make_plural(size_t ctr, const string &word, const string &ending = "s") {
     return (ctr > 1) ? word + ending : word;
}

// if list used instead, no need for erase since it is default called unique
// member call
void elimDups(vector<string> &words)
{
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
    print(cout, words);
}

void biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(words); // alphabetize words order and remove dups
    // sort words by size and alphabetize same size
    stable_sort(words.begin(), words.end(), 
      [] (const string &a, const string &b)
          { return a.size() < b.size();});
// get an iterator to the first element whose size() is >= sz
    auto wc = find_if(words.begin(), words.end(), [sz] (const string &a)
       { return a.size() >= sz; });
    auto count = words.end() - sz;
    cout << count << " "
         << make_plural(count, "word", "s")
         << " of length " << sz
         << " or longer" << endl;
         // print words of the given size or longer, each one followed by a space
    for_each(wc, words.end(), [] (const string &s)
       { cout << s << " ";});
    cout << endl;
}

int main()
{
    vector<string> vs2{"fox", "the", "jumps", "over", 
      "quick", "red", "slow", "red", "turtle"}

    elimDups(vs);
    elimDups(vs2);

    return 0;
 }

Upvotes: 0

Views: 6470

Answers (1)

Zeeshan Akhter
Zeeshan Akhter

Reputation: 476

The statement in function void biggies(vector<string> &words, vector<string>::size_type sz) on line number 38

auto count = words.end() - sz;

The part words.end() gives you an iterator and then you are subtracting a scalar which again gives you an iterator.

So the variable count is a iterator and the operator << is not defined for iterator.

What you can do is:

 auto count = words.size() - sz; // a number;

or

auto count = words.end() - wc; // a number;

Upvotes: 4

Related Questions