首页 > 解决方案 > 为什么我会收到“clang++:错误 - 没有匹配的函数调用”’”?

问题描述

我在 C++ 中有一个简单的函数,它检查给定元素是否存在于给定数组中并相应地返回true或返回false。这是代码示例以及我如何尝试使用它:

#include <iostream>

using namespace std;

bool find_elem(string array[], string elem)
{
    return (find(begin(array), end(array), elem) != end(array));
}

int main(void)
{
    string names[] = {"Bob", "Bill", "Sally", "Mark"}

    if (find_elem(names, "Bob))
    {
        cout << "Bob is in the array" << endl;
    }
    else
    {
        cout << "Bob is not in the array" << endl;
    }
    
    return 0;
}

我在find_elem()函数的返回语句上收到一条错误消息clang++: error - no matching function for call to 'begin',以及另外两个对end. 我不确定为什么会发生这种情况,因为这行代码在main()我的程序功能中运行良好,但如果我尝试在其他任何地方使用它,它会返回错误。有人可以解释为什么会发生这种情况以及我该如何解决吗?我最近才开始学习 C++,因此非常欢迎有关如何更好地做到这一点的示例或建议。

标签: c++clang++

解决方案


推荐阅读