简体   繁体   English

C ++构造函数问题//继承的类

[英]C++ Constructor problems // Inherited class

Initial problem solved. 最初的问题解决了。 (which was a redefinition error) (这是一个重新定义错误)

Furthermore, I don't know how to create a Student constructor which delivers the following: 此外,我不知道如何创建提供以下内容的Student构造函数:

This is how I want my program / constructors to work: 这就是我希望程序/构造函数起作用的方式:

//main.cpp
Student s1(4015885);
s1.print();
Student s2("Test", "Student", 22, 5051022);
s2.print();

The output should be as follows: 输出应如下所示:

Standart
Name
18
4015885

Test
Student
22
5051022

s1 works, but s2 doesn't work, because I don't have a fitting constructor s1有效,但是s2不起作用,因为我没有合适的构造函数

This is my Person.h 这是我的Person.h

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

class Person {

public:
    Person(string name = "Standard", string surname =  "Name", int age**strong text** = 18);                              

    //GET
    //SET
    void print();

protected:
    string name, surname;
    int age;
};

Person.cpp 人.cpp

#include <iostream>
#include <string>
#include "Person.h"
using namespace std;

Person::Person(string n, string s, int a) : name(n), surname(s), age(a)
{
    cout << "Person constructor" << endl;
}
void Person::print() {
    cout << name << endl << surname << endl << age << endl << endl;
}

Student.h 学生

#pragma once
//#ifndef PERSON_H
//#define PERSON_H
#include "Person.h"

class Student : public Person {
public:

    Student(int matrikel_nummer);

    int get_matriculation_number();

    void set_matriculation_number(int matriculation_number) {
        this->matriculation_number = matriculation_number;
    }

    void print();

private:
    int matriculation_number;

};
//#endif

Student.cpp 学生.cpp

#include <iostream>
#include <string>
#include "Student.h"
using namespace std;

Student::Student(int matrikel_nummer) : Person(name, surname, age)

{
    cout << "Student constructor" << endl;
    this->matriculation_number = matriculation_number;
}

void Student::print() {
    cout << name << endl
            << surname << endl
            << age << endl 
            << matriculation_number << endl << endl;
}

In Student.h you have Student.h

Student(int matriculation_number) : Person(name, surname, age){};

Which declares and defines a constructor for Student . 声明并定义Student的构造函数。 Then in Student.cpp you have 然后在Student.cpp

Student::Student(int matrikel_nummer) : Person(vorname, nachname, alter)

{
    cout << "Student constructor" << endl;
    this->matrikel_nummer = matrikel_nummer;
}

Which redefines the same constructor. 它重新定义了相同的构造函数。

You either need to get rid of the constructor definition in the class 您要么需要摆脱类中的构造函数定义

Student(int matriculation_number) : Person(name, surname, age){};
//becomes
Student(int matriculation_number);

or get rid of the constructor in the cpp file. 或摆脱cpp文件中的构造函数。

Also name, surname, age, vorname, nachname, alter do not appear anywhere in the code you have provided. 此外name, surname, age, vorname, nachname, alter不您所提供的代码的任何地方出现。 This will not compile unless they declared somewhere else. 除非他们在其他地方声明,否则不会编译。

EDIT: 编辑:

From the comments it looks like your Student constructor should be 从评论看来,您的Student构造函数应该是

Student(string n, string s, int a, int mn) : Person(n, s, a), matriculation_number(mn) {}

And you can put that in the header and you do not need a constructor definition in the cpp file. 您可以将其放在标题中,而在cpp文件中不需要构造函数定义。

Your error is here: 您的错误在这里:

Student(int matriculation_number) : Person(name, surname, age){};

Remove the {} and the base ctor call: 删除{}和基本ctor调用:

Student(int matriculation_number);

You already defined the constructor body of student: 您已经定义了student的构造函数体:

Student(int matriculation_number) : Person(name, surname, age){};

That is already a complete implementation of the constructor body, you should write this in the header: 那已经是构造函数主体的完整实现,您应该在标头中编写:

Student(int matriculation_number);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM