Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem with my code which i dont understand. Here is my code:
C++
#include <vector>
#include 
#include <iostream>
using namespace std;

struct findifdouble {
    map<int, string=""> count_of_vowels;
    explicit findifdouble(map<int, string="">& val) : count_of_vowels(val) {}
    bool operator()(int& val) {
        for (auto& x : count_of_vowels)
            if (x.first > val)
                return true;
            else
                return false;
    }
};

int main() {
    int index = 8;
    map<int, string=""> myvec;
    myvec.insert(map<int, string="">::value_type(125, "hello"));
    findifdouble myvc(myvec);
    auto it = find_if(myvec.begin(), myvec.end(),findifdouble((myvec)));

}


The error says:In template: no matching function for call to object of type 'findifdouble', in line 23(this line: auto it = find_if(myvec.begin(), myvec.end(),findifdouble((myvec))); )

Can anyone tell me what this error means, and how i can fix it

What I have tried:

I have tried to search the problem up but find no results
Posted
Updated 19-Sep-21 16:54pm
v3
Comments
Richard MacCutchan 19-Sep-21 6:35am
   
I cannot quite figure out this code, but I notice that you seem to be mixing types. Your operator() function is declared to expect an int& item, but you are passing it objects of type map<int, string>
Rick York 19-Sep-21 19:57pm
   
I find the using statement to be very helpful. For this, it could be something like this :
using mapintstring = std::map< int, std::string >;

Dereferencing iterator of std::map<KeyType, ValueType> gives you reference to key-value pair: std::pair<int const, std::string> therefore it's also the type a callable you provide to std::find_if must accept. In your example findifdouble callable accepts int& and that's why compiler is complaining. You have to change signature of operator () to:

bool operator()(std::pair<const int, std::string> const& val) { ... }

Note: both consts are important here.
   
At a quick glance

bool operator()(int& val) {
    for (auto& x : count_of_vowels)
        if (x.first > val)
            return true;
        else
            return false;
}
Should be:
bool operator()(int& val) {
    for (auto& x : count_of_vowels) {
        if (x.first > val)
            return true;
    }
    return false;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900