c++ 重载函数的多个实例匹配参数类型
c++ more than one instance of overloaded function matches argument type
我的函数 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_front
是 string
类型。 op
在这种情况下只是我创建的结构-如果需要我可以 post,但我觉得在这种情况下它是不必要的。
我的问题是为什么会出现此错误,因为上面的函数调用应该唯一调用第二种方法 header: bool contains(std::unordered_map<char, op>& um, std::string& s){
因为 opmap
的类型与第一个参数,q_front
的类型是string
.
更新:
完整错误信息:
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)
MRE:
#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, "+");
Overload 1 无法匹配,因为您的第二个参数,即 "+"
。它的类型是 const char[2]
并且无法匹配到 char
.
重载 2 和 3 无法匹配,因为 "+"
的类型具有 const 限定符,但在这两个重载中,您的 string
作为非常量引用传递。
因此,要解决您的问题,您应该:
- 将
"+"
更改为 '+'
以使用第一个重载。
- 将
std::string &
更改为 const std::string &
以使用重载 2。
我的函数 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_front
是 string
类型。 op
在这种情况下只是我创建的结构-如果需要我可以 post,但我觉得在这种情况下它是不必要的。
我的问题是为什么会出现此错误,因为上面的函数调用应该唯一调用第二种方法 header: bool contains(std::unordered_map<char, op>& um, std::string& s){
因为 opmap
的类型与第一个参数,q_front
的类型是string
.
更新:
完整错误信息:
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)
MRE:
#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, "+");
Overload 1 无法匹配,因为您的第二个参数,即 "+"
。它的类型是 const char[2]
并且无法匹配到 char
.
重载 2 和 3 无法匹配,因为 "+"
的类型具有 const 限定符,但在这两个重载中,您的 string
作为非常量引用传递。
因此,要解决您的问题,您应该:
- 将
"+"
更改为'+'
以使用第一个重载。 - 将
std::string &
更改为const std::string &
以使用重载 2。