WSS_ 2020-04-04 14:24 采纳率: 37.5%
浏览 4283
已采纳

为什么会出现[Error] no matching function for call to 'Student::Student()'???

#include
#include
#pragma warning(disable:4996)
using namespace std;

class Person

{

protected:
char name[10];

char sex;
public:
Person(const char name[], char sex) :sex(sex)

{

    strcpy(this->name, name);

}

};

class DoubleArray

{

protected:

double score;

public:

DoubleArray(double score) :score(score) {}
DoubleArray(DoubleArray &a)

{

    score=a.score;
} 
void print() const
{

    cout << score;

}
int operator-()// 重载取反运算符 operator-,使Student类中的cheat函数可正确运行 

{

    return -score;

} 

};

class Student : public Person

{

private:

int id;
char name[20];
DoubleArray score;

public:
static int count;
~Student()
{

    count--; 

}

void cheat(DoubleArray& ds) 
{ 

    score = -ds; 

} // 若作弊,则分数为负
Student(Student &s,DoubleArray& sc);
Student(int id, const char* name, char sex, DoubleArray& sc);
Student(int id, const char* name, char sex, double ds);
void disp();

};
Student::Student(int id, const char* name, char sex, DoubleArray& sc):Person(name,sex),score(sc)
{

count+=1;

}
Student::Student(int id, const char* name, char sex, double ds):Person(name,sex),score(ds)
{

count+=1;

}
void Student::disp()
{

cout<<"(id, name, sex, score)="<<"("<<id<<","<<name<<","<<sex<<",";
score.print();
cout<<")"<<endl;

}
// 类外实现Student类的成员函数,令main函数得以正确运行并输出相应的结果
int main()
{

cout << "count=" << Student::count << endl;
DoubleArray sc(97);
Student s[3]={Student(1,"Ann",'F',sc),Student(2,"Mike",'M',79),Student(3,"Lucy",'F',sc)};
s[2].cheat(sc);
for (int i = 0; i < 3; i++)
s[i].disp();
cout << "count=" << Student::count << endl;
return 0;

}

  • 写回答

2条回答 默认 最新

  • meehotel 2020-04-06 15:08
    关注

    解决办法:删除DoubleArray的拷贝构造函数。

    同Xcode警告 !No matching constructor for initialization of 'Student'同问啊!为什么说** 没有用于初始化“Student”的匹配构造函数**
    图片说明
    (宁也是大车埋土学校的?
    btw说一个小问题你的两个student构造函数id没初始化 id(id)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵