我目前正在创建一个名为OOP学生主管的程序。该程序应该打开文件,读取信息,通过类发送信息并正确显示结果。我目前正在从main手动创建信息作为测试。

这是我的代码:

main.cpp

#include <iostream>
#include <string>
#include <fstream>

#include "School_Academics.h"

using namespace std;


int main(){
    School_Academics n;
     Name name = Name("jack", "lopez");
     Address address = Address("123 some street", "Apt. A", "somecity", "somestate","somezipcode");
    Date dob = Date("01", "12", "1990");//date of birth
    Date dog = Date("07", "11", "2014");//date of graduation
    School_Academics acad = School_Academics("3.64", "25", dog);
    cout<<n.getStudent()<<endl;
    Student n2 = Student(name, address, dob, acad);
    cout<<n2.getStudent();

}

Student.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED

#include "Address.h"
#include "School_Academics.h"
#include "Name.h"
#include "Date.h"

using namespace std;

class Student{
    private:
        Name name;
        Address address;
        Date dob;
        School_Academics acad;

    public:
        Student();
        Student(Name name, Address address, Date dob, School_Academics acad);
        string getStudent();

};
#endif // STUDENT_H_INCLUDED

Student.cpp
#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>

#include "Student.h"
using namespace std;

Student::Student(){
    name = Name("","");
    address = Address("","","","","");
    dob = Date("0","0","0");
    acad = School_Academics("0","0","0");
}
Student::Student(Name name, Address address, Date dob, School_Academics acad){
    Student::name = name;
    Student::address = address;
    Student::dob = dob;
    Student::acad = acad;

}

string Student::getStudent(){
    name.getName();
    address.getAddress();
    acad.getSchool_Academics();
    dob.getDate();
}

School_Academics.cpp
#include <string>
#include <iostream>
#include <cstdlib>
#include <sstream>

#include "School_Academics.h"

School_Academics::School_Academics(){
    GPA = "0";
    credits = "0";
    Date DOG = Date("0","0","0");
}

School_Academics:: School_Academics(string GPA, string credits, Date DOG){
    School_Academics::GPA = GPA;
    School_Academics::credits = credits;
    School_Academics::DOG = DOG;
}


string School_Academics::getSchool_Academics(){
    stringstream ss;
    ss <<"School Academics: GPA: "<<GPA<<" credits: "<<credits<<" Graduate: "<<DOG.getDate()<<endl;
    return ss.str();
}

Date.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

#include "Date.h"

using namespace std;

Date::Date(){
    day = "0";
    month = "0";
    year = "0";
}

Date::Date(string day, string month, string year){
    Date::day = day;
    Date::month = month;
    Date::year = year;
}

string Date::getDate(){
    stringstream ss;
    ss <<"Date: "<< day << "/" << month << "/" << year << endl;
    return ss.str();
}

问题:

我目前在从main.cpp发送信息并通过Student.cpp传递信息并获取class Student{};来从Date.cpp获取信息时遇到问题。

收到错误:
|13|error: no matching function for call to 'School_Academics::School_Academics(const char [1], const char [1], const char [1])'|
如果可以,请帮助我解决错误。先感谢您。

最佳答案

错误来自您的学生ctor,您具有以下情况

acad = School_Academics("0","0","0");

未定义具有3个字符的School_Academics ctor。这会导致错误。

您可能打算通过上面创建的日期:
Student::Student(){
    name = Name("","");
    address = Address("","","","","");
    dob = Date("0","0","0");
    acad = School_Academics("0","0",dob);
    // or acad = School_Academics("0","0",date());
}

关于c++ - 尝试调用构造函数,收到 “no matching function call”编译错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28907052/

10-17 01:45