C++ 派生问题no overloaded function takes 2 parameters

#include<iostream>#include<string>usingnamespacestd;classperson{protected:intnumber;s... #include<iostream>
#include<string>
using namespace std;
class person
{
protected:
int number;
string name;
int salary;
public:
person();
person(int nu,string n,int s)
{number=nu;name=n;salary=s;}
int calculate();
void display();
};
void person::display()
{
cout<<number<<" "<<name<<" "<<salary;
}
person::person()
{
number=0;
name="";
salary=0;
}
int person::calculate()
{
}
class teacher:virtual public person
{
protected:
int level1;
int hour1;
public:
teacher(int nu,string n,int s,int l1,int h1):person(nu,n,s)
{level1=l1;hour1=h1;}
};
class student:virtual public person
{
protected:
int hour2;
int wage;
public:
student();
student(int nu,string n,int s,int h2,int w):person(nu,n,s),hour2(h2),wage(w){}
};
student::student()
{
hour2=0;
wage=0;
}
class teachassistant:public teacher,public student
{
protected:
int level2;
int hour3;
public:
teachassistant(int nu,string n,int s,int l1,int h1,int h2,int w,int l2,int h3):person(nu,n,s),teacher(l1,h1),student(h2,w),level2(l2),hour3(h3)//系统告诉我问题出在这
{}
};
展开
 我来答
宋景贤
2013-04-14 · 超过30用户采纳过TA的回答
知道答主
回答量:81
采纳率:0%
帮助的人:65.3万
展开全部
错误信息:error C266error C2661: 'student::student' : no overloaded function takes 2 parameters, 'teacher::teacher' : no overloaded function takes 2 parameters
从错误信息可以知道问题所在。
你在teachassistant的初始化列表中调用了teacher和student的构造函数,而你在teacher、student这两个类中并没有编写两个参数的构造函数,参数不匹配,所以调用出错。
解决方案是:要么给teacher、student这两个定义分别定义两个参数的构造函数,或者在调用的时候提供足够多的实参。
追问
那应该怎么添加?麻烦您给我事例下好么,比如student的那个。我用缺省的不好使啊。
追答
在成员函数那里加下面这样一个函数:
student(int h2,int w):person(),hour2(h2),wage(w) {}

这样就只需要两个参数了。
这个叫做函数重载,函数名一样,但是形参的个数或者类型不一样,调用函数时会根据所提供的实参的个数和类型选择对应的函数。
丰木jyy
2013-04-14 · 超过13用户采纳过TA的回答
知道答主
回答量:31
采纳率:0%
帮助的人:36.4万
展开全部
student::student()和person::person()前加上class,你没有主函数(就不明白你的主要方向),我只能帮你解答到这儿了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式