我是C++的新手,我正在做这个简单的作业,需要查找最大值和最小值,它一直给我带来不希望的id错误。这是代码,非常感谢。

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int min(int a, int b, int c, int d)
{
    int result = a;
    if (b < result) result = b;
    if (c < result) result = c;
    if (d < result) result = d;
    return result;
}

int main()
{
    int x;
    x = min(2,6,3,4);
    cout << " The result is " << x;
}

int max( int a, int b, int c, int d); #expected unqualified id
{
    int max = a;
    if (b > result) result = b;
    if (c > result) result = c;
    if (d > result) result = d;
    return max;
}

int main1()
{
    int x;
    x = min(2,6,3,4);
    cout << " The result is " << x;
}

最佳答案

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int result;

int min(int a, int b, int c, int d)
{
    result = a;
    if (b < result) result = b;
    if (c < result) result = c;
    if (d < result) result = d;
    return result;
}

int max(int a, int b, int c, int d)
{
    result = a; //before-edit "int max = a"
    if (b > result) result = b;
    if (c > result) result = c;
    if (d > result) result = d;
    return result;
}

int main()
{
    int Min,Max;
    Min = min(2,6,3,4);
    cout << " The result for minimum is " << Min << endl;
    Max = max(2,6,3,4);
    cout << " The result for maximum is " << Max;
}

max(a,b,c,d)函数上,您仅将的值传递给,并且没有通过 min()之类的其他情况,请不要懒惰地查看复制并粘贴后的代码。

之所以出现预期的不合格ID错误,是因为您在此行的末尾加上了分号。
int max(int a, int b, int c, int d);
{
    ...
}

关于c++ - 尝试查找最大值和最小值以及预期的不合格id错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48614888/

10-13 05:05