简体   繁体   English

C ++错误-呼叫没有匹配功能

[英]C++ Error - no matching function for call

I am having some problems trying to compile my code because of an error. 由于出现错误,我在尝试编译代码时遇到一些问题。

It is in function mergeSort , line: merge(a, from, mid, to); 它在功能mergeSort中 ,行: merge(a, from, mid, to); :

Invalid arguments ' Candidates are: 2 merge(#0, #0, #1, #1, #2, #3) 2 merge(#0, #0, #1, #1, #2) ' no matching function for call to merge(std::vector >&, int&, int&, int&) 无效的参数'候选者为:2 merge(#0,#0,#1,#1,#2,#3)2 merge(#0,#0,#1,#1,#2)'没有匹配的功能调用合并(std :: vector>&,int&,int&,int&)

    void mergeSort(vector<string> &a, int from, int to)
    {
        if (from == to)
        {
            return;
        }
        int mid = (from + to) / 2;

        mergeSort(a, from, mid);
        mergeSort(a, mid + 1, to);
        merge(a, from, mid, to);

    } // end mergeSort

     void merge(vector<string> &a, int from, int mid, int to)
    {
        int n = to - from + 1; 
        vector<string> b(n); 
        int i1 = from;
        int i2 = mid + 1; 
        int j = 0; 

        while (i1 <= mid && i2 <= to)
        {
            if (a[i1].compare(a[i2]) < 0)
            {
                b[j] = a[i1];
                i1++;
            }
            else
            {
                b[j] = a[i2];
                i2++;
            }
            j++;
        }

        while (i1 <= mid)
        {
            b[j] = a[i1];
            i1++;
            j++;
        }

        while (i2 <= to)
        {
            b[j] = a[i2];
            i2++;
            j++;
        }

        for (j = 0; j < n; j++)
        {
            a[from + j] = b[j];
        }
    } 

     int main() {
        vector<string> v = {"Apple", "Fruit", "Banana", "apple", "4apples", "applesauce", "3bananas", "\"apple\""}

        //  Print original vector
                cout << "******Original*******"<< endl;
                for (vector<string>::size_type i = 0; i < v.size(); ++i)
                {

                  cout << v[i] << endl;
                }

               mergeSort(v, 0, v.size() - 1);

                cout << "******MERGE SORTED*******"<< endl;
                for (vector<string>::size_type i = 0; i < v.size(); ++i)
                {

                      cout << v[i] << endl;
                }

        }

The standard C++ library has its own std::merge() functions. 标准C ++库具有自己的std::merge()函数。 Those are the candidates you see described in the compiler error as not matching. 这些是您看到的在编译器错误中描述为不匹配的候选对象。 It does not appear the compiler is even considering your merge() function. 似乎编译器甚至没有考虑您的merge()函数。 This implies that you have not declared your function before trying to use it, and that you have a using namespace std statement in your code. 这意味着您在尝试使用函数之前尚未声明函数,并且您的代码中具有using namespace std语句。

You need to declare your function. 您需要声明您的功能。 And you need to either get rid of the using statement or else declare your function in its own namespace, and then have mergeSort() be explicit about which namespace to pull merge() from. 并且您需要摆脱using语句,或者在自己的名称空间中声明函数,然后使mergeSort()明确说明要从哪个名称空间提取merge()

Declare the functions before calling them. 在调用函数之前先声明它们。 Add the following line (declaration of merge function), before your mergeSort function's definition. 在您的mergeSort函数的定义之前,添加以下行( merge函数的声明)。

void merge(vector<string> &a, int from, int mid, int to);

Read about declarations and definitions in C++: 阅读有关C ++中的声明和定义的信息:

https://www.geeksforgeeks.org/difference-between-definition-and-declaration/ https://www.geeksforgeeks.org/difference-between-definition-and-declaration/

What is the difference between a definition and a declaration? 定义和声明之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM