-2

So i have created this example to simplify the question. I have 3 classes where 2 inherits from the same one.

the problems are as described down bellow. with the main errpr beeing uninitialized member variables and non standard syntax error requiring a pointer

MAN CLASS

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

TEACHER CLASS

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

STUDENT CLASS

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

};

Main

    int main(){

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

ERRORS

enter image description here

Error (active)  E1696   cannot open source file
Warning C26495  Variable 'Man::age' is uninitialized. Always initialize a member variable (type.6). 
Warning C26495  Variable 'Student::age' is uninitialized. Always initialize a member variable (type.6). 
Warning C26495  Variable 'Man::age' is uninitialized. Always initialize a member variable (type.6). 
Warning C26495  Variable 'Student::age' is uninitialized. Always initialize a member variable (type.6). 
Error   C3867   'Teacher::getName': non-standard syntax; use '&' to create a pointer to member  
Error   C2664   'Man::Man(Man &&)': cannot convert argument 1 from 'int' to 'std::string'   

added error log in text format on request.

Stanley
  • 2,434
  • 18
  • 28
  • @ThomasSablik thanks for the information on why decided to downvote, added errors in text format – Stanley Jul 14 '19 at 19:46
  • `Man::Man(string, int) {` doesn't do what you think it does. You need `Man::Man(string name, int age) {`. I recommend getting a C++ book ASAP. – n. m. could be an AI Jul 14 '19 at 19:47
  • 1
    `getName` is a method. You forgot the braces. Your class properties and the constructor arguments should be named different. What you try to do is called shadowing and some people recommend to avoid it. You can enable compiler warnings for shadowing. – Thomas Sablik Jul 14 '19 at 19:48
  • Use a constructor initializer list to avoid the error with missing argument names. – Thomas Sablik Jul 14 '19 at 19:54
  • Thank you for putting the error messages in. The problem is not uninitialized data members (although that may be problematic in the future), but the problems shown here are `E1696` (which I don't know if we can do anything about. Certainly not without more info about your environment), `C3867 ` (which I'm not entirely sure what that's about), and `C2664` (which doesn't make sense at all, given the code you have currently provided. I don't see that line anywhere in your code here). –  Jul 14 '19 at 21:17
  • So, what is the question? And what research did you already do to find an answer? – Walter Jul 14 '19 at 21:33

1 Answers1

2

Here is an example of your code after I fixed the errors

#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);
}
  • I added braces to the call of getName.
  • I added constructor initializer lists.
  • I fixed the order of the arguments in the call to the constructor.
  • I removed shadowing.
  • I removed using namespace std;.
  • I named the arguments of the constructors.
  • I replaced std::endl with '\n'. You don't need to flush your buffer on every line.
  • I removed the age in teacher. Man already has an age
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • This does not answer the question (partly because there is none) but provides a working code. – Walter Jul 14 '19 at 21:34
  • Thank you for the great explanation, i am very new to c++ and i polymorphism. Shadowing is the way we were taught in school, i will try to avoid that in further projects. Flushing the buffer on every line is also something we were taught in school but i will avoid that from now aswell and i'll look at some tutorials explaining when and when not to use it. Also why would you remove using namespace std;? altough that did remove alot of errors on uninitialized member variables. and the getName braces was a beginner mistake, but glad you called me out on it. Thank you for your input. – Stanley Jul 14 '19 at 22:09
  • @stanley I saw at least 5 problems at SO that were solved by removing `using namespace std;`. As a beginner you can't know all classes, constants and functions that are defined in namespace `std`. You could define a new function or class with the same name as something in that namespace. This will generate errors that aren't that obvious to solve. Also it is easier to distinguish custom functions from standard functions. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Thomas Sablik Jul 14 '19 at 22:18
  • @ThomasSablik Thank you! great link you provided aswell, I will definately stop using it! – Stanley Jul 14 '19 at 22:22