简体   繁体   English

C ++:子类中的交叉引用

[英]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 学生.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 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: 在来自university.cpp:2的文件中:

/bachelorstudent.h:7: error: invalid use of incomplete type 'struct Student' /bachelorstudent.h:7:错误:无效使用了不完整的类型'struct Student'

/university.h:5: error: forward declaration of 'struct Student' /university.h:5:错误:“结构学生”的前向声明

In file included from university.cpp:3: 在university.cpp:3包含的文件中:

/magisterstudent.h:7: error: invalid use of incomplete type 'struct Student' /magisterstudent.h:7:错误:无效使用了不完整的类型'struct Student'

/university.h:5: error: forward declaration of 'struct Student' /university.h:5:错误:“结构学生”的前向声明

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. 根据许多问答论坛,“在标头中转发,包括在cpp文件中”是避免任何可能出现问题的最佳做法。 So what's wrong with my code? 那么我的代码有什么问题呢?

Someone (CodeFuller) supposed a correct answer and removed it. 有人(CodeFuller)认为正确的答案并将其删除。 Why? 为什么?

His answer was: 他的回答是:

As far as BachelorStudent is inherited from Student , I must add 至于BachelorStudent是从Student继承的,我必须添加

#include "student.h"

in a header of bachelorstudent.h bachelorstudent.h的标题中

It works. 有用。 Thanks a lot! 非常感谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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