
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)//系统告诉我问题出在这
{}
}; 展开
#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)//系统告诉我问题出在这
{}
}; 展开
2个回答
展开全部
错误信息: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这两个定义分别定义两个参数的构造函数,或者在调用的时候提供足够多的实参。
从错误信息可以知道问题所在。
你在teachassistant的初始化列表中调用了teacher和student的构造函数,而你在teacher、student这两个类中并没有编写两个参数的构造函数,参数不匹配,所以调用出错。
解决方案是:要么给teacher、student这两个定义分别定义两个参数的构造函数,或者在调用的时候提供足够多的实参。
追问
那应该怎么添加?麻烦您给我事例下好么,比如student的那个。我用缺省的不好使啊。
追答
在成员函数那里加下面这样一个函数:
student(int h2,int w):person(),hour2(h2),wage(w) {}
这样就只需要两个参数了。
这个叫做函数重载,函数名一样,但是形参的个数或者类型不一样,调用函数时会根据所提供的实参的个数和类型选择对应的函数。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询