if statement error! [closed]
I got the following error:
error: no matching function for call to 'max(int&, int&, int&, int&)'
My code is:
#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);
}
The line I got the error on was on the 74th line:
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)
This was all in C++ just in case you don't see the tags or recognize the code.
max
gets just two arguments, so you have to composite it from multiple calls.
int maxGd = max(max(manudgd,rmadgd),max(lagalgd,acmilgd));
if(maxGd == manudgd || maxGd == lagalgd || maxGd == acmilDg) {
// ...
}
std::max
takes two arguments, not an arbitrary number of arguments.
max
takes two arguments. You may want to see max_element. By the way, your code may need some refactoring - please read Clean Code from Robert C. Martin.
That's because there is no max
function that takes four arguments. max
takes only two arguments. You can write one such function yourself, using the one that already exists:
int max(int a, int b, int c, int d) {
return max(max(a,b),max(c,d));
}
In C++ std::max()
takes only two arguments.
Eh? There is no max()
function that I am aware of which takes four arguments! There is a two arg version, std::max()
in <algorithm>
.
If this is your own, implement it.
Because there is no such function called max
that takes 4 arguments in the c++ and you haven't defined one!
I don't see in the code where you've implemented max(int&, int&, int&, int&)
method.
In C++ <algorithm>
library, you can find std::max()
declared as follows:
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 );
The max() function has not been defined in the scope of the call. Add a definition and compile again.
Side note: max() is a macro in some compilers, but then with 2 arguments instead of 4.
精彩评论