You will always need to declare all of the classes before your main function, and before you use the forward declarated class's properties.
To solve this specific case: declare the teacher class before the main function (as you can see in previous answers).
For some different cases, when you have to use class object inside another class object, you can declare the dependency class before, and you can use a c++ technique called forward declaration.
For example, Car's class can be included in teacher's class (and in some countries in both teacher & student- depend on max student age). So you can implement this class before student & teacher (in some cases of double dependency it can be tricky), but you can also do something like this:
#include <memory>
#include <iostream>
using namespace std;
class Car; // forward declaration of Car class.
class Student
{
public:
Student(); // don't implement yet!
unique_ptr<Car> car; // not implemented yet
int rollno=100;
};
class Teacher
{
public:
Teacher(); // don't implement yet!
unique_ptr<Car> car; // not implemented yet
int teacherNo=999;
};
// Implement forward declaration class, before using in inside a function
class Car {
public:
string name;
};
// Implement Student & Teacher constructors
Student::Student() {
car = unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}
Teacher::Teacher() {
car = unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}
int main()
{
Student s;
Teacher t;
cout<< s.rollno << endl;
cout<< t.teacherNo << endl;
t.car->name = "Car name";
s.car->name = "Car name2";
cout<< s.car->name << endl; // Only after Car's class implementation you can access Car's object
cout<< t.car->name << endl; // Only after Car's class implementation you can access Car's object
}
EDIT: Forward declaration in separated files
Student.h
#ifndef PROJECT_STUDENT_H
#define PROJECT_STUDENT_H
#include <memory>
class Car; // forward declaration of Car class.
class Student
{
public:
Student(); // don't implement yet!
std::unique_ptr<Car> car; // not implemented yet
int rollno=100;
};
#endif //PROJECT_STUDENT_H
Student.cpp
#include "Student.h"
#include "Car.h" // In case of forward declaration, usually it's better to write includes in .h file, but sometimes it's not possible (in cases of double dependency).
// Implement Student constructors
Student::Student() {
car = std::unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}
Teacher.h
#ifndef PROJECT_TEACHER_H
#define PROJECT_TEACHER_H
#include <memory>
class Car;
class Teacher
{
public:
Teacher(); // don't implement yet!
std::unique_ptr<Car> car; // not implemented yet
int teacherNo=999;
};
#endif //PROJECT_TEACHER_H
Teacher.cpp
#include "Teacher.h"
#include "Car.h" // In case of forward declaration, usually it's better to write includes in .h file, but sometimes it's not possible (in cases of double dependency).
// Implement Teacher constructors
Teacher::Teacher() {
car = std::unique_ptr<Car>(new Car()); // this line can be written only after Car's class implementation!
}
Car.h
#ifndef PROJECT_CAR_H
#define PROJECT_CAR_H
#include <string>
// Implement forward declaration class, before using in inside a function
class Car {
public:
std::string name;
};
#endif //PROJECT_CAR_H
Car.cpp - Empty
main.cpp
#include <memory>
#include <iostream>
#include "Student.h"
#include "Teacher.h"
#include "Car.h"
using namespace std;
int main()
{
Student s;
Teacher t;
cout<< s.rollno << endl;
cout<< t.teacherNo << endl;
t.car->name = "Car name";
s.car->name = "Car name2";
cout<< s.car->name << endl;
cout<< t.car->name << endl;
}