未初始化的成员变量和非标准语法使用&创建指向成员的指针

时间:2019-07-14 19:38:19

标签: c++ c++11

因此,我创建了此示例以简化问题。我有3个类,其中2个是从同一个继承的。

问题如下所述。主要错误是未初始化的成员变量,并且非标准语法错误需要指针

MAN CLASS

<div class="form-group col-md-6">
  <label for="portada">Foto Receta</label>
  <input type="file" accept=".png, .jpg" (change)="onUpload($event)">
</div>

教师班

class Man {
private:
    int age;
public:
    string name;

    Man() { //constructor
        this->age = 0;
        this->name = "default";
        cout << "created Man in default constructor" << endl;
    }
    Man(string, int);// constructor
};
Man::Man(string, int) {
    this->name = name;
    this->age = age;
    cout << "created Man: " << this->name << endl;
}

学生班

class Teacher : Man {
public:
    string field;
    Teacher(string, string) {
        this->name = name;
        this->field = field;
        cout << "created Teacher: " << this->name << endl;
    }

    Teacher() {
        this->field = "not declared";
        cout << "created Teacher in default constructor" << endl;
    }

    string getName() {
        return name;
    }
};

主要

class Student : Man {
public:
    Teacher tutor;
    int age;

    Student(string, Teacher, int) {
        this->name = name;
        this->tutor = tutor;
        this->age = age;
        cout << "created student: " << this->name << " teacher is: " << this->tutor.getName << endl;
    }

};

错误

enter image description here

    int main(){

    Man erlichBlachman(12, "Erlich Blachman");
    Teacher richardHendricks("Richard Hendricks", "compression algorithms");
    Student dinesh("Dinesh", richardHendricks, 27);
    Student gilfoyle("Gilfoyle", richardHendricks, 32);
    }

根据要求添加了文本格式的错误日志。

1 个答案:

答案 0 :(得分:2)

这是我修复错误后的代码示例

#include <iostream>
#include <string>

class Man {
private:
    int age;
public:
    std::string name;

    Man() : age(0), name("default") { //constructor
        std::cout << "created Man in default constructor\n";
    }
    Man(std::string, int);// constructor
};
Man::Man(std::string aName, int aAge) : age(aAge), name(aName) {
    std::cout << "created Man: " << name << '\n';
}

class Teacher : Man {
public:
    std::string field;
    Teacher(std::string aName, std::string aField) : field(aField) {
        name = aName;
        std::cout << "created Teacher: " << name << '\n';
    }

    Teacher() : field("not declared") {
        std::cout << "created Teacher in default constructor\n";
    }

    std::string getName() {
        return name;
    }
};

class Student : Man {
public:
    Teacher tutor;

    Student(std::string aName, Teacher aTutor, int aAge) : Man(aName, aAge), tutor(aTutor) {
        std::cout << "created student: " << name << " teacher is: " << tutor.getName() << '\n';
    }

};

int main(){
    Man erlichBlachman("Erlich Blachman", 12);
    Teacher richardHendricks("Richard Hendricks", "compression algorithms");
    Student dinesh("Dinesh", richardHendricks, 27);
    Student gilfoyle("Gilfoyle", richardHendricks, 32);
}
  • 我在getName的呼叫中添加了括号。
  • 我添加了构造函数初始化器列表。
  • 我固定了对构造函数的调用中的参数顺序。
  • 我删除了阴影。
  • 我删除了using namespace std;
  • 我命名了构造函数的参数。
  • 我将std::endl替换为'\n'。您不需要在每一行上都刷新缓冲区。
  • 我取消了老师的年龄。人已经有年龄了