c++ 多个重载函数实例匹配参数类型

RJC810

我已经重载了我的函数contains三遍

// 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();
}

// returns true if string s is contained in unordered map um
bool contains(std::unordered_map<char, op>& um, std::string& s){
    return s.length() == 1 && contains(um, s[0]); 
}

// returns true if string s is contained in unordered map um
bool contains(std::unordered_map<std::string, func>& um, std::string& s){
    return um.find(s) != um.end(); 
}

每个重载函数的参数都不同。然而,从这一行(contains(opmap, q_front))我得到了错误:more than one instance of overloaded function "contains" matches the argument list.

作为参考,opmap是 类型std::unordered_map<char, op>,并且q_frontstringop在这种情况下只是我创建的结构 - 如果需要我可以发布,但我觉得在这种情况下这是不必要的。

我的问题是为什么我会收到这个错误,因为上面的函数调用应该唯一地调用第二个方法头:bool contains(std::unordered_map<char, op>& um, std::string& s){因为类型opmap匹配第一个参数,类型q_frontstring

更新:

完整的错误信息:

more than one instance of overloaded function "contains" matches the argument list: -- function "contains(std::unordered_map<char, op, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, op>>> &um, std::string s)" (declared at line 48 of "/Users/raleighclemens/Documents/Calc_cpp/calc.h") -- function "contains(std::unordered_map<char, op, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, op>>> &um, std::string &s)" (declared at line 49) -- argument types are: (std::unordered_map<char, op, std::hash<char>, std::equal_to<char>, std::allocator<std::pair<const char, op>>>, std::string)C/C++(308)

雷:

#include <iostream>
#include <string>
#include <functional>
#include <unordered_map>

#define LEFT 0
#define RIGHT 1
#define UNARY 0
#define BINARY 1

struct op{
    char symbol;
    uint8_t precedence;
    uint8_t assoc;
    uint8_t type;

    std::function<double (double, double)> ashley;

};

struct func{
    std::string symbol;
    uint8_t type;

    std::function<double (double, double)> ashley;
};

bool contains(std::unordered_map<char, op>& um, char c){
    return um.find(c) != um.end();
}

// returns true if string s is contained in unordered map um
bool contains(std::unordered_map<char, op>& um, std::string& s){
    return s.length() == 1 && contains(um, s[0]); 
}

// returns true if string s is contained in unordered map um
bool contains(std::unordered_map<std::string, func>& um, std::string& s){
    return um.find(s) != um.end(); 
}


int main(int argc, char** argv){

    std::unordered_map<char, op> opmap;
    op op1{'+', 2, LEFT, BINARY, [=] (double a, double b){return a + b;}};
    opmap.emplace('+', op1);

    std::cout << contains(opmap, "+");
    
古普塔

您希望哪个重载将您的呼叫与下面的行相匹配?

std::cout << contains(opmap, "+");

由于您的第二个参数,即"+". 它的类型是const char[2]并且不能匹配到char

重载 2 和 3 无法匹配,因为 的类型"+"具有 const 限定符,但在这两个重载中,您string作为非常量引用传递。

因此,要解决您的问题,您应该:

  • 更改"+"'+'使用第一个重载。
  • 更改std::string &const std::string &使用重载 2。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

c ++模板-多个重载函数实例与参数列表匹配

C ++创建头文件时,多个重载函数实例与参数列表匹配

映射中的C ++结构作为值-错误“没有重载函数的实例与参数列表匹配”

C ++和SFML,没有重载函数“ sf :: RenderWindow.draw()”的实例与参数列表匹配

c ++ getline()错误,没有重载函数“ getline”的实例与参数列表匹配

通过参数C ++的类型专门研究重载的构造函数

如何重载具有多个参数的函数?C ++

C ++没有构造函数的实例与参数列表匹配。

C ++:静态断言参数包与函数类型匹配

使用GCC在C中进行函数重载-具有多个参数的函数

C++ 模板 - 找不到匹配的重载函数,无法推导出“T”的模板参数

C ++线程“未找到匹配的重载函数”

在C ++中使用带有const类型输入参数的函数内部使用运算符重载

C ++编译时检查,可以使用某种类型的参数调用重载函数

c#-从具有未知参数类型的函数中调用良好的重载方法

C ++重载函数作为模板参数

以结构继承作为参数重载函数C ++

指针类型的C ++模板函数重载

函数返回类型重载 C++

错误:没有匹配的函数调用'sf::RenderWindow::draw(<未解析的重载函数类型>)'| C++ 中的 SFML

当只有一个参数c#时,是否可以通过反射创建实例并指定重载的“ params”构造函数?

即使重载函数的参数计数不匹配,编译器是否应该实例化所有参数相关类型

C ++模板类:没有构造函数的实例与参数列表匹配

C ++ / Threading:没有构造函数“ std :: thread :: thread”的实例>匹配参数列表

如何修复C ++中的“构造函数实例不匹配参数列表”错误?

没有重载函数的实例与参数列表匹配

获取C ++函数参数的类型

C++ 具有特定类型可变参数的多个函数参数

在C ++中的模板实例化中将带有构造函数的类用作类型参数