试图调用构造函数,接收“无匹配函数调用”编译错误

时间:2015-03-06 20:25:23

标签: c++

我目前正致力于创建一个名为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])'|

如果可以,请帮我解决错误。提前谢谢。

1 个答案:

答案 0 :(得分:3)

错误来自您的学生,您有以下

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());
}