When I try to run this code:
Instructor.cpp:
#include "Instructor.h" #include "Person.h" using std::string; Instructor::Instructor() { Person(); salary = 0; } Instructor::Instructor(string n, string d, string g, int s) { Person(n, d, g); salary = s; } void Instructor::print() { if (gender == "M") std::cout << "Mr. "; else std::cout << "Ms. "; Person::print(); std::cout << " Instructor, Salary: " << salary << std::endl; }
Instructor.h:
#include <iostream> #include <string> class Instructor: public Person { public: Instructor(); Instructor(std::string n, std::string d, std::string g, int s); virtual void print(); protected: int salary; };
Person.h:
#include <iostream> #include <string> class Person { public: Person(); Person(std::string n, std::string d, std::string g); virtual void print(); protected: std::string name; std::string dob; std::string gender; };
I get the following errors:
In file included from Instructor.cpp:1:0: Instructor.h:5:1: error: expected class-name before '{' token { ^ Instructor.cpp: In member function 'virtual void Instructor::print()': Instructor.cpp:16:6: error: 'gender' was not declared in this scope if (gender == "M") ^ Instructor.cpp:20:16: error: cannot call member function 'virtual void Person::print()' without object Person::print();
All three errors confuse me. If the instructor class was obtained from Person and inside the gender field gender, then why do I get error: 'gender' was not declared in this scope , as well as error: cannot call member function 'virtual void Person::print()' without object ?
It seems to me that I am doing something obviously wrong here, for example, including files incorrectly or something like that. Any help is appreciated.
c ++ netbeans
user2628156
source share