C++一个奇怪的问题??no appropriate default constructor available

王朝知道·作者佚名  2010-10-14
窄屏简体版  字體: |||超大  
 
分類: 電腦/網絡 >> 程序設計 >> 其他編程語言
 
問題描述:

#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;

}

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航