#include<iostream>
class football
{
private:
int *win,*lost,m,max;
public:
football(int n);
~football();
void input(int *winb,int *lostb,int n);
int score();
int winball();
};
football::football(int n=50)
{
max=n;
win=new int[max];
lost=new int[max];
m=0;
}
football::~football()
{
delete []win;
delete []lost;
}
void football::input(int *winb,int *lostb,int n)
{
for (int i=0;i<n;++i)
{
win[i]=winb[i];
lost[i]=lostb[i];
}
m=n;
}
int football::score()
{
int total_score=0;
for (int i=0;i<m;++i)
{
if (win[i]-lost[i]>0) total_score+=3;
if (win[i]-lost[i]==0) total_score+=1;
}
return total_score;
}
int football::winball()
{
int total_goal=0;
for (int i=0;i<m;++i) total_goal+=win[i]-lost[i];
return total_goal;
}
int main()
{
int w[10]={3,1,4,2,3,1,2,3,4,2},l[10]={2,2,3,3,1,3,2,1,3,2};
football football_test;
football_test.input(w,l,10);
std::cout<<"该队的积分为:"<<football_test.score()<<std::endl;
std::cout<<"该队的净胜球数为:"<<football_test.winball()<<std::endl;
return(0);
}
编译后在football football_test;一行报错:no appropriate default constructor available
为什么啊?
參考答案:给形成一个默认值,只能在申明中!
class football
{
private:
int *win,*lost,m,max;
public:
football(int n=50); //申明中提供
~football();
void input(int *winb,int *lostb,int n);
int score();
int winball();
};
football::football(int n) //这里是定义,不应该提供int n=50
{
max=n;
win=new int[max];
lost=new int[max];
m=0;
}