Syed Abdul Wahab Syed Abdul Wahab - 3 months ago 13
C++ Question

Errors in class relationships in c++

I am quite new to the concept of class relationships and wrote a program to test it out. However, it is giving me some errors.

There are 5 header files for classes University,Dept,Teacher,Student and Course and a .cpp file involved. I have implemented composition between University and Dept and bidirectional association between Dept and Student, Teacher, Course

uni.h

#pragma once
//#include"dept.h"
class University
{
public:
University(string,int);
void setDepartmentName(string,int);
Dept* getDeptAddress(int);
~University();
private:
Dept* departments;
int totalDepts;
string name;
};

University::University(string name1,int num) :name(name1)
{
departments = new Dept[num];
totalDepts=num;
}

void University::setDepartmentName(string name,int depNo)
{
departments[depNo].setName(name);
}

Dept* University::getDeptAddress(int i)
{
return &(departments[i]);
}

University::~University()
{
delete[] departments;
}


dept.h

#pragma once
//class Student;
//class Teacher;
//class Course;
#include"course.h"
#include"teacher.h"
#include"student.h"
class Dept
{
public:
Dept();
string getName();
void setName(string);
~Dept();

private:
Student** students;
Course** courses;
Teacher** teachers;
string name;
int noOfStudents, noOfTeachers, noOfCourses;
};

Dept::Dept()
{

}

string Dept::getName() {
return name;
}

void Dept::setName(string name1) {
name = name1;
}
Dept::~Dept()
{
}


course.h

#pragma once
class Dept;
//#include"dept.h"
class Course
{
public:
Course(string, string);
void assignDept(Dept*);
string getDeptName();
~Course();

private:
Dept* department;
string code;
string name;

};

Course::Course(string code1, string name1) :code(code1), name(name1)
{}
void Course::assignDept(Dept * dep)
{
department = dep;
}
string Course::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}

Course::~Course()
{}


student.h

#pragma once
#include"dept.h"
class Student
{
public:
Student(string,string);
void assignDept(Dept*);
string getDeptName();
~Student();

private:
Dept* department;
string rollNo, name;
};

Student::Student(string rNo,string name1):name(name1),rollNo(rNo)
{}


void Student::assignDept(Dept *dep)
{
department = dep;
}

string Student::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();
}


Student::~Student()
{
}


teacher.h

#pragma once
//#include"dept.h"
class Teacher
{
public:
Teacher(string);
void assignDept(Dept*);
string getDeptName();
~Teacher();
private:
Dept* department;
string name;
};

Teacher::Teacher(string name1) :name(name1)
{}


void Teacher::assignDept(Dept *dep)
{
department = dep;
}

string Teacher::getDeptName()
{
//statement giving error: 'Use of undefined type 'Dept'' & 'left of -> must point to class'
return department->getName();



}

Teacher::~Teacher()
{
}


source.cpp

#include<iostream>
using namespace std;
#include<string>
#include"dept.h"
#include"course.h"
#include"teacher.h"
#include"student.h"
#include"uni.h"

int main()
{

University u1("FAST",3);
u1.setDepartmentName("CS", 0);
u1.setDepartmentName("EE", 1);
u1.setDepartmentName("CV", 2);

Student s1("l144049", "Syed Abdul Wahab");
Course c1("cs1", "ITC");
Teacher t1("abc");

c1.assignDept(u1.getDeptAddress(0));
t1.assignDept(u1.getDeptAddress(1));
s1.assignDept(u1.getDeptAddress(2));

cout << c1.getDeptName()<<endl;
cout << t1.getDeptName() << endl;
cout << s1.getDeptName() << endl;

cin.get();
return 0;

}


However, if i #include 'dept.h' in student.h, course.h and teacher.h, it gives me errors on 'Dept' namely 'identifier 'Dept' is undefined'.
Any help would me greatly appreciated!

Answer

The problem is you have a circular dependency: teacher.h includes dept.h which includes teacher.h. This can't work.

To fix it, use "forward declarations" in your header files, and move your implementations to .cpp files.