简体   繁体   English

模板参数扣除/替换失败:

[英]Template argument deduction/substitution failed:

I have a compile error when using "output_array()" in a templated member function, the following code is a simple example:在模板化成员 function 中使用“output_array()”时出现编译错误,以下代码是一个简单示例:

#include <iostream>

using namespace std;

int menu()
{
    int opt;
    cout << "\n**********MENU**********";
    cout << "\n1. Print the Array in reverse order";
    cin >> opt;
    return opt;
}

void input_array(int b[], int n)
{
    cout << "\nEnter the elements of the array";
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    return;
}

void output_array(int c[], int n)
{
    for (int j = 0; j < n; j++)
    {
        cout << c[j] << " ";
    }
    return;
}



void rev_arr(int b[], int n)
{
    int start = 0, end = n - 1;

    cout << "Array before reversing : " << output_array(b, n);

    for (int i = 0; i < n / 2; i++)
    {
        float temp = b[start];
        b[start] = b[end];
        b[end] = temp;
        start++;
        end--;
    }

    cout << "Array after reversing : " << output_array(b, n);
    return;
}

void task(int b[], int n, int opt)
{
    switch (opt)
    {
    case 1:
        rev_arr(b, n);
        break;
    default:
        cout << "!!! Sorry Wrong Choice !!!";
    }
    return;
}

int main()
{
    int size;
    int a[50];
        cout << "\n You can Enter 50 elements in the array.\nHow much you want to enter?";
        cin >> size;

        input_array(a, size);
        int opt = menu();
        task(a, size, opt);
        

    return 0;
}

The output_array(int*, int) is a printing function, not an opertot<< overlaod. output_array(int*, int)是打印 function,而不是opertot<< Therefore, your can't place output_arrry(b, n) after operator<< .因此,您不能将output_arrry(b, n)放在operator<<之后。 Simple separate into two statments:简单分成两个语句:

Change改变

cout << "Array after reversing : " << output_array(b, n); // Not good.

into进入

cout << "Array after reversing : ";  output_array(b, n); // ok.

Or you have to write an operator<< overloading, give rules for operator<< to print you array.或者你必须编写一个operator<<重载,为operator<<提供规则来打印你的数组。 But, before doing this, you have to bind the size n in your array (as some kind of structure).但是,在这样做之前,您必须在数组中绑定大小n (作为某种结构)。 The size is needed to be used in the function operator<< . function operator<<中需要使用该大小。

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

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