简体   繁体   中英

C++: Cross reference in subclasses

I've got a trouble with cross-reference in the following situation:

Suppose there are Students (bachelors, magisters) at the University:

university.h

#pragma once

#include <QDebug>

class Student;

class University : public QObject
{
    Q_OBJECT
public:
    explicit University(QObject *parent = 0);
    QList<Student*> students;
};

student.h

#pragma once

class University;

class Student : public QObject
{
    Q_OBJECT
public:
    explicit Student(QString name,  University *parent = 0);
};

bachelor.h

#pragma once

class Student;
class University;

class BachelorStudent : public Student
{
    Q_OBJECT
public:
    explicit BachelorStudent(QString name, University *parent = 0);
};

magister.h

#pragma once

class Student;
class University;

class MagisterStudent : public Student
{
    Q_OBJECT
public:
    explicit MagisterStudent(QString name, University *parent = 0);
};

The implementation looks like this:

university.cpp

#include "university.h"
#include "bachelorstudent.h"
#include "magisterstudent.h"
#include "student.h"

University::University(QObject *parent) : QObject(parent) {
    students.append(new BachelorStudent("tommy", this));
    students.append(new MagisterStudent("bobby", this));
    qDebug() << students;
}

student.cpp

#include "student.h"
#include "university.h"

Student::Student(QString name, University *parent) : QObject(parent) {
    setObjectName(name);
}

bachelorstudent.cpp

#include "bachelorstudent.h"
#include "student.h"
#include "university.h"

BachelorStudent::BachelorStudent(QString name, University *parent) : Student(name, parent)
{}

magisterstudent.cpp

#include "magisterstudent.h"
#include "student.h"
#include "university.h"

MagisterStudent::MagisterStudent(QString name, University *parent) : Student(parent)
{}

... and simple main program:

#include <QApplication>
#include "university.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    new University(&a);
    return a.exec();
}

Unfortunately a compiler throws:

In file included from university.cpp:2:

/bachelorstudent.h:7: error: invalid use of incomplete type 'struct Student'

/university.h:5: error: forward declaration of 'struct Student'

In file included from university.cpp:3:

/magisterstudent.h:7: error: invalid use of incomplete type 'struct Student'

/university.h:5: error: forward declaration of 'struct Student'

that means incorrect forward class declaration in a case of circular dependencies. According to lots of Q&A-forums the way "forwards in headers, includes in cpp-files" is a best practice to avoid any possible problems. So what's wrong with my code?

Someone (CodeFuller) supposed a correct answer and removed it. Why?

His answer was:

As far as BachelorStudent is inherited from Student , I must add

#include "student.h"

in a header of bachelorstudent.h

It works. Thanks a lot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM