ayaka_s_honey 2022-10-31 21:58 采纳率: 100%
浏览 103
已结题

“没有与参数列表相匹配的构造函数实例”

我使用的IDE是visual studio2019.
现我定义了一个学生类student。并且完成了构造函数与析构函数的声明和定义。但我在主函数调用构造函数时,编译器报错:“没有与参数列表相匹配的构造函数实例”。
编译器报错显示:主函数中的student stu(1,"wang",18,86)中的“1”没有与参数列表相匹配的构造函数实例

img

以下是我的代码

#include<iostream>
using namespace std;
class student
{
private:
    int id;
    char* name;
    int age;
    float score;
public:
    student(int i, char* n, int a, float s);
    student(student&);
    ~student();
    void printstu();
};
student::student(int i, char* n, int a, float s)
{
    cout << "constructing..." << endl;
    id = i;
    name = new char[strlen(n) + 1];
    age = a;
    score = s;
    if (name != 0)
    {
        strcpy(name, n);
    }
}
student::student(student&s)
{
    cout << "copy constructing..." << endl;
    id = s.id;
    age = s.age;
    score = s.score;
    name = new char[strlen(s.name) + 1];
    if (name != 0)
    {
        strcpy(name, s.name);
    }
}
student::~student()
{
    cout << "destructing..." << endl;
    delete[]name;
    name = 0;
}
void student::printstu()
{
    cout << "学号:" << id << "姓名:" << name;
    cout << "年龄:" << age << "成绩:" << score << endl;
}
void main()
{
    student stu(1, "wang", 18, 86);
    stu.printstu();
}

请教解答,orz

  • 写回答

1条回答 默认 最新

  • 叶落花枯 2022-10-31 22:02
    关注

    是不是因为第四个参数类型没对上,输入整型,构造函数是浮点型

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月9日
  • 已采纳回答 11月1日
  • 修改了问题 10月31日
  • 创建了问题 10月31日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号