错误 C2664:“print_result”:无法将参数 1 从“int (__cdecl *)(int,int,in

error C2664: 'print_result' : cannot convert parameter 1 from 'int (__cdecl *)(int,int,int)' to 'int'

本文关键字:int in cdecl result print C2664 错误 参数      更新时间:2023-10-16
#include<iostream>
using namespace std;
bool is_different(int x, int y, int z);
int max_of_three(int x, int y, int z);
int greater_of_two(int x, int y);
int min_of_three(int x, int y, int z);
int smaller_of_two(int x, int y);
void print_result(int min_of_three, int max_of_three);
int main ()
{
    int x, y, z;
    while(1==1)
    {
        cout << "Please input 3 different integers." << endl;
        cin >> x >> y >> z;
        if (is_different (x, y, z))
        {break;}
    }
    print_result(min_of_three, max_of_three);//error here
}
bool is_different (int x, int y, int z)
{
    if (x!=y && x!=z && y!=z)
    {
        return true;
    }
    else
    {return false;}
}   
int greater_of_two (int x, int y)
{
    if (x > y)
        return x;
    if (y > x)
        return y;
    return 0;
}

int max_of_three (int x, int y, int z)
{
    return greater_of_two(greater_of_two(x, y) , z);
}
int smaller_of_two (int x, int y)
{
    if (x < y)
        return x;
    if (y < x)
        return y;
    return 0;
}
int min_of_three (int x, int y, int z)
{
    return smaller_of_two(smaller_of_two(x, y) , z);
}
void print_result (int min_of_three, int max_of_three)
{
    cout << "The minimum value of the three is " << min_of_three << endl;
    cout << "The maximum value of the three is " << max_of_three << endl;
}

我正在尝试编写一个程序来查找赋值的三个不同输入整数的最大值和最小值,这是我第一次处理函数,我不明白错误的含义。

您正在尝试传递作为签名的函数指针的min_of_threemax_of_three

int

(*)(int, int, int)

print_result期望int论点。

你想要这个:

print_result(min_of_three(x,y,z), max_of_three(x,y,z));

函数min_of_three(x,y,z)max_of_three(x,y,z)返回int,然后用作print_result函数的参数。