Inheritance using header files.

So I'm trying to practice basic inheritance. I have one main .cpp file with several classes and I tried to create a header file with another class derived from the parent class on the main file. Everything on the main file compiled and ran fine.

When I made the header file and included it, I got the error "expected class name before '{' token." Why doesn't recognize Person as a class name?? All the other classes on the main .cpp recognized it. I'm very confused lol. None of the functions of the parent class Person are passed to the derived class Student. They work with the other classes though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

//main.cpp//
#include <iostream>
 using namespace std;
#include "Student.h"


 class Person
 {
 	 public:
	 	 string job;
	 	 int age;

	 	 person(string job, int age) {}
	 	 void display() {
	 		 cout << "My job is a " << job << endl;
	 		 cout << "I am " << age << " years old" << endl;
	 	 }
	 	 void walk() {
	 		 cout << "I can walk" << endl;
	 	 }
	 	 void talk() {
	 		 cout << "I can talk" << endl;
	 	 }
 };

 class Teacher : public Person
 {
 	 public:
	 	 void teach() {
	 		 cout << "I teach students!" << endl << endl;
	 }
 };

 class Counselor : public Person
 {
 	 public:
	 	 void counsel() {
	 		 cout << "I provide counseling to students!" << endl << endl;
	 	 }
 };

 class Janitor : public Person
 {
 	 public:
	 	 void clean() {
	 		 cout << "I clean up after everyone..." << endl << endl;
	 	 }
 };


 	int main() {

 		Teacher Davis;
 		Davis.job = "Math Teacher";
 		Davis.age = 29;
 		Davis.display();
 		Davis.teach();

 		Teacher Johnson;
 		Johnson.job = "History Teacher";
 		Johnson.age = 45;
 		Johnson.display();
 		Johnson.talk();
 		Johnson.teach();

 		Teacher Sims;
 		Sims.job = "Science Teacher";
 		Sims.age = 39;
 		Sims.display();
 		Sims.walk();
 		Sims.teach();

 		Janitor Smith;
 		Smith.job = "Janitor";
 		Smith.age = 51;
 		Smith.talk();
 		Smith.walk();
 		Smith.display();

 		Counselor Sanchez;
 		Sanchez.job = "Counselor";
 		Sanchez.age = 25;
 		Sanchez.talk();
 		Sanchez.display();

 		Student John;
 		John.job = "student";
 		John.age = 12;
 		John.study();
 		John.display();
 	}

//Student.h//
#ifndef STUDENT_H_
#define STUDENT_H_
#include <iostream>

class Student : public Person
{
	public:
		void study() {
			cout << "I am a student. I am here to study and learn!" << endl << endl;
	}
};


#endif /* STUDENT_H_ */
Here are the errors-

../Student.h:13:1: error: expected class-name before '{' token
13 | {
| ^
../classes_objects.cpp: In member function 'int Person::person(std::string, int)':
../classes_objects.cpp:12:34: warning: no return statement in function returning non-void [-Wreturn-type]
12 | person(string job, int age) {}
| ^
../classes_objects.cpp: In function 'int main()':
../classes_objects.cpp:87:9: error: 'class Student' has no member named 'job'
87 | John.job = "student";
| ^~~
../classes_objects.cpp:88:9: error: 'class Student' has no member named 'age'
88 | John.age = 12;
| ^~~
../classes_objects.cpp:90:9: error: 'class Student' has no member named 'display'
90 | John.display();
| ^~~~~~~
make: *** [subdir.mk:20: classes_objects.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

17:31:32 Build Failed. 5 errors, 1 warnings. (took 1s.192ms)

> #include "Student.h"
...
> class Person
At the point you include the header file, the compiler hasn't yet learnt what a 'Person' is.

Topic archived. No new replies allowed.