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 receive these 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 of these errors are confusing me. If the Instructor class was derived from Person, and within Person the gender field is protected, then why am I receiving error: ‘gender’ was not declared in this scope
, as well as error: cannot call member function ‘virtual void Person::print()’ without object
?
I feel like I'm doing something obviously wrong here, such as including the files incorrectly or something like that. Any help is appreciated.