模板函数给出"no matching function for call"错误
Template function gives "no matching function for call" error
关于堆栈溢出的第一个问题:)我对C++相对较新,从未使用过模板,所以如果我在做一些愚蠢的事情,请原谅我。我有一个模板函数,可以梳理列表并检查常规类型的指定元素。这样我就可以指定它是在寻找字符串、整数还是其他什么。
template <class T>
bool inList(T match, std::string list)
{
int listlen = sizeof(list);
for (int i = 0; i <= listlen; i++) {
if (list[i] == match) return true;
else continue;
}
return false;
};
这是我对inList()
的呼唤。 testvec
是一个包含几个元素的字符串向量,包括"test":
if (inList<string>("test", testvec))
cout << "success!";
else cout << "fail :(";
令我沮丧和困惑的是,在编译时,我被以下错误打耳光:
error: no matching function for call to 'inList(const char [5], std::vector<std::basic_string<char> >&)'
我做错了什么?:(
[编辑]我忽略了模板定义在全局命名空间中。(这是一个简单的测试程序,看看我的模板是否可以工作,显然:(
这是因为没有办法从 std::vector 转换为 std::string。 相反,您需要做的是抽象集合的概念。
这样做是为了让人们可以传入他们想要的任何集合类型。 他们可以使用数组、向量、字符串、列表、双...只要它符合集合的"概念"(这里希望 C++1X 带有概念! 他们甚至可以使用自己的内部专门优化的集合类型。 这就是模板的美妙之处。
使用 C++11(适用于任何标准集合、基元数组和用户定义类型(:
template<class elem_t, class list_t>
bool in_list(const elem_t& elem, const list_t& list) {
for (const auto& i : list) {
if (elem == i) {
return true;
}
}
return false;
}
编辑:这是一个非标准扩展,用于推断std::initializer_list作为模板参数,因此请提供显式覆盖:
template<class elem_t>
bool in_list(const elem_t& elem, std::initializer_list<elem_t> list) {
for (const auto& i : list) {
if (elem == i) {
return true;
}
}
return false;
}
使用此版本,您可以像这样称呼它:
int main() {
std::vector<int> a = {1, 2, 3, 4, 5};
std::cout << in_list(3, a) << std::endl;
std::string b = "asdfg";
std::cout << in_list('d', b) << std::endl;
std::cout << in_list('d', "asdfg") << std::endl;
std::cout << in_list(3, {1, 2, 3, 4, 5}) << std::endl;
return 0;
}
对于我们这些仍在 C++98 中的人来说,这将适用于字符串和向量,以及一些用户定义的类型。 不过,它不适用于原始数组。
template<class elem_t, class list_t>
bool in_list_98(const elem_t& elem, const list_t& list) {
list_t::const_iterator end = list.end(); //prevent recomputation of end each iteration
for (list_t::const_iterator i = list.begin(); i < end; ++i) {
if (elem == *i) {
return true;
}
}
return false;
}
或者,你可以采用 STL 风格:
template<class elem_t, class iterator_t>
bool in_list_stl(const elem_t& elem, iterator_t begin, iterator_t end) {
for (iterator_t i = begin; i < end; ++i) {
if (elem == *i) {
return true;
}
}
return false;
}
//call like std::string s = "asdf"; in_list_stl('s', s.begin(), s.end());
如果我犯了一个错误,对不起,我现在没有运行我的编译器......
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- ESP32-AsyncUDP own handler function for udp.onPacket
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 尝试在矢量上使用擦除功能时"no matching function for call"
- "error: no matching function for call to ‘push_back(char [6])" .如何解决此代码中的错误?
- 使用 std::min "no matching function for call to ‘min(<brace-enclosed initializer list>)’"时出错
- C++字符串.替换生成"No matching function for call"错误
- 如何修复从基类继承两次时"error: no matching function for call to"
- "No matching function for call to ... "在源中使用 CGImage 时C++
- 编译代码时"No matching function for call"。[QTS天线端口打开读取]