如果声明错误!

时间:2011-07-18 13:53:45

标签: c++ if-statement

我收到以下错误:

  

错误:没有匹配函数来调用'max(int&,int&,int&,int&)'

我的代码是:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main()
{
    srand(time(NULL));
    int g1l= rand()%5;
    int g1r= rand()%5;
    int g2l= rand()%5;
    int g2r= rand()%5;
    int g3l= rand()%5;
    int g3r= rand()%5;
    int g4l= rand()%5;
    int g4r= rand()%5;
    int g5l= rand()%5;
    int g5r= rand()%5;
    int g6l= rand()%5;
    int g6r= rand()%5;
    int manudw = 0;
    int manudd = 0;
    int manudl = 0;
    int lagalw = 0;
    int lagald = 0;
    int lagall = 0;
    int rmadw = 0;
    int rmadd = 0;
    int rmadl = 0;
    int acmilw = 0;
    int acmild = 0;
    int acmill = 0;

    int manudp = ((manudw*3)+(manudd*1));
    int lagalp = ((lagalw*3)+(lagald*1));
    int rmadp = ((rmadw*3)+(rmadd*1));
    int acmilp = ((acmilw*3)+(acmild*1));
    string manud = "Manchester United";
    string lagal = "Los Angeles Galaxy";
    string rmad = "Real Madrid";
    string acmil = "AC Milan";
    string place1 = "Manchester United  |";
    string place2 = "Real Madrid        |";
    string place3 = "AC Milan           |";
    string place4 = "Los Angeles Galaxy |";
    int counter=0;
    int round1;
    int round2;
    int round3;
    int manudgd = 0;
    int rmadgd = 0;
    int acmilgd = 0;
    int lagalgd = 0;
    do
    {
        int manudp = ((manudw*3)+(manudd*1));
        int lagalp = ((lagalw*3)+(lagald*1));
        int rmadp = ((rmadw*3)+(rmadd*1));
        int acmilp = ((acmilw*3)+(acmild*1));
        int manudgd = ((g1l+g3r+g6r)-(g1r+g3l+g6l));
        int rmadgd = ((g2l+g3l+g5r)-(g2r+g3r+g5l));
        int acmilgd = ((g2r+g4l+g6l)-(g2l+g4r+g6r));
        int lagalgd = ((g1r+g4r+g5l)-(g1l+g4l+g5r));
        cout<<place1<<" "<<manudw<<" | "<<manudd<<" | "<<manudl<<" | "<<manudp<<" | "<<manudgd<<"  |"<<endl<<
              place2<<" "<<rmadw<<" | "<<rmadd<<" | "<<rmadl<<" | "<<rmadp<<" | "<<rmadgd<<"  |"<<endl<<
              place3<<" "<<acmilw<<" | "<<acmild<<" | "<<acmill<<" | "<<acmilp<<" | "<<acmilgd<<"  |"<<endl<<
              place4<<" "<<lagalw<<" | "<<lagald<<" | "<<lagall<<" | "<<lagalp<<" | "<<lagalgd<<"  |"<<endl<<endl;
//        if(max(manudgd,rmadgd,lagalgd,acmilgd)==manudgd || max(manudgd,rmadgd,lagalgd,acmilgd)==rmadgd || max(manudgd,rmadgd,lagalgd,acmilgd)==lagalgd || max(manudgd,rmadgd,lagalgd,acmilgd)==acmilgd)
        if(counter==0)
        {
            for(round1=0;round1<=0;round1++)
            {
                cout<<"Manchester United "<<g1l<<"-"<<g1r<<" Los Angeles Galaxy"<<endl;
                cout<<"Real Madrid "<<g2l<<"-"<<g2r<<" AC Milan"<<endl<<endl;
                int manudgd = ((g1l)-(g1r));
                int rmadgd = ((g2l)-(g2r));
                int acmilgd = ((g2r)-(g2l));
                int lagalgd = ((g1r)-(g1l));
                if(g1l>g1r)
                {
                    manudw++;
                    lagall++;
                }
                else if(g1l<g1r)
                {
                    lagalw++;
                    manudl++;
                }
                else if(g1l==g1r)
                {
                    manudd++;
                    lagald++;
                }
                if(g2l>g2r)
                {
                    rmadw++;
                    acmill++;
                }
                else if(g2l<g2r)
                {
                    rmadl++;
                    acmilw++;
                }
                else if(g2l==g2r)
                {
                    rmadd++;
                    acmild++;
                }
            }
        }
        else if(counter==1)
        {
            for(round2=0;round2<=0;round2++)
            {
                cout<<"Real Madrid "<<g3l<<"-"<<g3r<<" Manchester United"<<endl;
                cout<<"AC Milan "<<g4l<<"-"<<g4r<<" Los Angeles Galaxy"<<endl<<endl;
                int manudgd = ((g1l+g3r)-(g1r+g3l));
                int rmadgd = ((g2l+g3l)-(g2r+g3r));
                int acmilgd = ((g2r+g4l)-(g2l+g4r));
                int lagalgd = ((g1r+g4r)-(g1l+g4l));
                if(g3l>g3r)
                {
                    rmadw++;
                    manudl++;
                }
                else if(g3l<g3r)
                {
                    manudw++;
                    rmadl++;
                }
                else if(g3l==g3r)
                {
                    manudd++;
                    rmadd++;
                }
                if(g4l>g4r)
                {
                    acmilw++;
                    lagall++;
                }
                else if(g4l<g4r)
                {
                    acmill++;
                    lagalw++;
                }
                else if(g4l==g4r)
                {
                    lagald++;
                    acmild++;
                }
            }
        }
        else if(counter==2)
        {
            for(round3=0;round3<=0;round3++)
            {
                cout<<"Los Angeles Galaxy "<<g5l<<"-"<<g5r<<" Real Madrid"<<endl;
                cout<<"AC Milan "<<g6l<<"-"<<g6r<<" Manchester United"<<endl<<endl;
                int manudgd = ((g1l+g3r+g6r)-(g1r+g3l+g6l));
                int rmadgd = ((g2l+g3l+g5r)-(g2r+g3r+g5l));
                int acmilgd = ((g2r+g4l+g6l)-(g2l+g4r+g6r));
                int lagalgd = ((g1r+g4r+g5l)-(g1l+g4l+g5r));
                if(g5l>g5r)
                {
                    lagalw++;
                    rmadl++;
                }
                else if(g5l<g5r)
                {
                    rmadw++;
                    lagall++;
                }
                else if(g5l==g5r)
                {
                    lagald++;
                    rmadd++;
                }
                if(g6l>g6r)
                {
                    acmilw++;
                    manudl++;
                }
                else if(g6l<g6r)
                {
                    acmill++;
                    manudw++;
                }
                else if(g6l==g6r)
                {
                    manudd++;
                    acmild++;
                }
            }
        }
        counter++;
    }
    while(counter!=4);
}

我收到错误的行是在第74行:

if(max(manudgd,rmadgd,lagalgd,acmilgd)==manudgd || max(manudgd,rmadgd,lagalgd,acmilgd)==rmadgd || max(manudgd,rmadgd,lagalgd,acmilgd)==lagalgd || max(manudgd,rmadgd,lagalgd,acmilgd)==acmilgd)

这完全是在C ++中,以防你没有看到标签或识别代码。

9 个答案:

答案 0 :(得分:6)

max只获得两个参数,因此您必须从多个调用中合成它。

int maxGd = max(max(manudgd,rmadgd),max(lagalgd,acmilgd));
if(maxGd == manudgd || maxGd == lagalgd || maxGd == acmilDg) {
    // ...
}

答案 1 :(得分:4)

std::max有两个参数,而不是任意数量的参数。

答案 2 :(得分:2)

max有两个参数。您可能希望看到max_element。顺便说一句,您的代码可能需要一些重构 - 请阅读Robert C. Martin的Clean Code

答案 3 :(得分:1)

那是因为没有max函数需要四个参数。 max只有两个参数。您可以使用已存在的函数自己编写一个这样的函数:

int max(int a, int b, int c, int d) {
    return max(max(a,b),max(c,d));
}

答案 4 :(得分:0)

在C ++ std::max()只需要两个参数。

答案 5 :(得分:0)

嗯?我知道没有max()函数需要四个参数! std::max()中有两个arg版本<algorithm>

如果这是您自己的,请实施它。

答案 6 :(得分:0)

因为没有这样的函数max在c ++中有4个参数而你没有定义一个!

答案 7 :(得分:0)

我没有在代码中看到您实现了max(int&, int&, int&, int&)方法。

在C ++ <algorithm>库中,您可以找到声明如下的std::max()

template <class T> const T& max ( const T& a, const T& b );
template <class T, class Compare>
  const T& max ( const T& a, const T& b, Compare comp );

答案 8 :(得分:0)

max()函数尚未在调用范围内定义。 添加定义并重新编译。

旁注:max()在某些编译器中是一个宏,但后面有2个参数而不是4个。