C++:子类中的交叉引用
C++: Cross reference in subclasses
在以下情况下我遇到了交叉引用的问题:
假设大学有学生(学士、硕士):
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);
};
实现如下所示:
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);
}
单身汉student.cpp
#include "bachelorstudent.h"
#include "student.h"
#include "university.h"
BachelorStudent::BachelorStudent(QString name, University *parent) : Student(name, parent)
{}
魔导师student.cpp
#include "magisterstudent.h"
#include "student.h"
#include "university.h"
MagisterStudent::MagisterStudent(QString name, University *parent) : Student(parent)
{}
...和简单的主程序:
#include <QApplication>
#include "university.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
new University(&a);
return a.exec();
}
不幸的是编译器抛出:
在包含自 university.cpp 的文件中:2:
/bachelorstudent.h:7: 错误:不完整类型的无效使用 'struct Student'
/university.h:5: 错误:'struct Student'
的前向声明
在包含自 university.cpp 的文件中:3:
/magisterstudent.h:7: 错误:不完整类型的无效使用 'struct Student'
/university.h:5: 错误:'struct Student'
的前向声明
这意味着在循环依赖的情况下不正确的前向 class 声明。根据许多问答论坛,"forwards in headers, includes in cpp-files" 的方式是避免任何可能出现的问题的最佳做法。那么我的代码有什么问题?
有人 (CodeFuller) 认为答案正确并删除了它。为什么?
他的回答是:
至于BachelorStudent继承自Student,我必须加上
#include "student.h"
在 bachelorstudent.h
的 header 中
有效。非常感谢!
在以下情况下我遇到了交叉引用的问题:
假设大学有学生(学士、硕士):
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);
};
实现如下所示:
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);
}
单身汉student.cpp
#include "bachelorstudent.h"
#include "student.h"
#include "university.h"
BachelorStudent::BachelorStudent(QString name, University *parent) : Student(name, parent)
{}
魔导师student.cpp
#include "magisterstudent.h"
#include "student.h"
#include "university.h"
MagisterStudent::MagisterStudent(QString name, University *parent) : Student(parent)
{}
...和简单的主程序:
#include <QApplication>
#include "university.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
new University(&a);
return a.exec();
}
不幸的是编译器抛出:
在包含自 university.cpp 的文件中:2:
/bachelorstudent.h:7: 错误:不完整类型的无效使用 'struct Student'
/university.h:5: 错误:'struct Student'
的前向声明在包含自 university.cpp 的文件中:3:
/magisterstudent.h:7: 错误:不完整类型的无效使用 'struct Student'
/university.h:5: 错误:'struct Student'
的前向声明这意味着在循环依赖的情况下不正确的前向 class 声明。根据许多问答论坛,"forwards in headers, includes in cpp-files" 的方式是避免任何可能出现的问题的最佳做法。那么我的代码有什么问题?
有人 (CodeFuller) 认为答案正确并删除了它。为什么?
他的回答是:
至于BachelorStudent继承自Student,我必须加上
#include "student.h"
在 bachelorstudent.h
的 header 中有效。非常感谢!