首页 > 解决方案 > 没有重载函数的实例与指定的类型匹配 (C++)

问题描述

#include <iostream>
#include <string>
using namespace std;

class student {
private:
    string sname;
    float gpa;
public:
    student(string name, float g = 0.0);
    ~student();
};

struct student {
    string name;
    int gpa;

    void display();
};

student::student(string name, float g) { // **this is where I get the error**
    cout << "student constructor is running..." << endl;
    student stu;
    stu.name += name;
    stu.gpa = g;
}

student::~student() {
    // cout << "Student No." << endl;
    cout << "student destructor is running..." << endl;
}

void student::display() {
    cout << "Student Name: " << name << endl;
    cout << "Student GPA: " << gpa << endl;
}

这是一个学生头文件。错误出现了:没有构造函数“student::student”的实例与参数列表匹配。我不明白为什么以及如何解决这个问题。

标签: c++error-handlingconstructor

解决方案


编译器将学生视为结构,更改学生结构名称


推荐阅读