mercredi 14 juillet 2021

function overloading in c++ for sets and maps

I have overloaded the method contains to check whether or not an element is contained in unordered set and unordered map. The code is as follows:

bool contains(std::unordered_set<std::string>* us, std::string s){
    return (*us).find(s) != (*us).end();
}

// returns true if char c is contained in unordered map um
bool contains(std::unordered_map<char, op>* um, char c){
    return (*um).find(c) != (*um).end();
}

However, when I use the method, i.e. contains(&opmap, input[i-1]) where opmap has definition std::unordered_map<char, op> opmap; and input is defined as std::string input, I get error

no instance of overloaded function "contains" matches the argument list -- argument types are: (std::unordered_map<char, <error-type>, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, <error-type>>>>, char)C/C++(3.

Note that op is a struct I created in the same file. I'm not sure why it isn't using the second overloaded method of contains. Also, why are the argument parameters listed in the error six long, when the arguments I put into contains are two long?

Aucun commentaire:

Enregistrer un commentaire