C++包含在一个循环中
请考虑以下三个简化文件:
Student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include "course.h"
class Student
{
private:
Course someCourse;
};
#endif
course.h:
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
private:
Student someStudent;
};
#endif
和 main.cpp:
#include "student.h"
int main();
这不会编译给我
错误 C2146:语法错误:缺少“;”在标识符“someStudent”之前
在更复杂的程序中,它会产生更多的错误(即使是正确的代码部分)。我猜设计是错误的:Student
包含Course
,而Course
包含Student
。我想用它来表示一个学生选修了几门课程,并且一门课程有几个学生(我在完整的程序中使用向量,为了简单起见,在这里避免使用它们)。有什么建议吗?这怎么可能?
预先感谢,弗拉德。
更新: 感谢您的快速回复。在 Course
类中向前声明 Student
类(并删除 #include "student.h"
)似乎可以完成这项工作。 抱歉,我认为这在这里并不重要,但事实上我在每个向量中使用 const 指针向量(因为学生不应该能够控制 Course
和 课程
不应该能够控制学生
),如下所示:
vector<const Student* const> students; // in Course class
Please consider the following three simplified files:
student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include "course.h"
class Student
{
private:
Course someCourse;
};
#endif
course.h:
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
private:
Student someStudent;
};
#endif
and main.cpp:
#include "student.h"
int main();
This wouldn't compile giving me
error C2146: syntax error : missing ';' before identifier 'someStudent'
It would produce a lot more errors (even for the correct portions of code) in a more complicated program. I guess the design is wrong: Student
includes Course
and Course
includes Student
. What I want to represent with it is that a student takes several courses and a course has has several students (I use vectors in a full program, avoided them here for simplicity). Any advice how this would be possible?
Thanks in advance, Vlad.
UPDATE:
Thanks for fast replies. Forward declaration of Student
class in Course
class (and removing #include "student.h"
) seems to do the job.
Sorry, I thought it wouldn't matter here, but in fact I am using vectors of const pointers in each of them (since a Student shouldn't be able to control a Course
and a Course
shouldn't be able to control a Student
), as:
vector<const Student* const> students; // in Course class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是循环的,只要您将
someCourse
和someStudent
声明为类Student
和Course
的非指针成员> 分别(正如您所做的那样),因为编译器看到Student
的定义,它需要知道它的大小,这反过来意味着它需要知道其所有成员的大小,包括 <代码>课程这是其中之一。但要知道Course
的大小,就需要知道Student
的大小。那就变成圆形了。因此,您需要通过将其中至少一个声明为指针来打破这个循环。例如,您可以这样做:
另请注意,当您将
pSomeCourse
声明为Course*
类型的指针时,不需要包含pSomeCourse
所在的头文件>课程已定义。只需向前声明类Course
就足够了,就像我在上面的代码中所做的那样。它之所以有效,是因为任何类的指针的大小都是相同的,并且编译器不需要知道类的大小,就可以知道指针的大小> 同级。换句话说,编译器甚至可以在不知道
sizeof(Course)
的情况下知道sizeof(Course*)
。This is going circular, as long as you declare
someCourse
andsomeStudent
as non-pointer members of classStudent
andCourse
respectively (as you've done), because the compiler sees the definition ofStudent
, it needs to know its size which in turn means, it needs to know the size of its all members, includingCourse
which is one of them. But to know the size ofCourse
, it needs to know the size ofStudent
. That becomes circular.So you need to break this circle, by declaring at least one of them as pointer. For example, you can do this:
Also note that when you declare
pSomeCourse
as pointer of typeCourse*
, you don't need to include the header file in whichCourse
is defined. Just forward declaration of the classCourse
is enough, as I did in the above code.The reason why it works because the size of pointer of any class is same, and the compiler need not know the size of the class, in order to know the size of the pointer of the same class. In other words, the compile can know
sizeof(Course*)
without even knowingsizeof(Course)
.除了 Nawaz 的回答之外:
如果您想从 Student 的成员访问 Course 的成员,您需要将 Course.h 包含在 .cpp - 您定义 Student 方法的文件中。
否则,使用 g++ 您将收到“无效使用不完整类型”之类的错误。
In addition to Nawaz answer:
if you want to access members of Course from members of Student, you need to include Course.h in the .cpp - File where you define the methods of Student.
With g++ you'll get an error like "invalid use of incomplete type" otherwise.
如果要链接两个类,则必须使用前向声明和指向类接口之一中声明类型的实例的指针。另一个接口可以保持不变,只要它包含成员变量的类型接口的声明即可。
course.h:
学生.h:
If you want to link two classes you will have to use forward declaration and a pointer to an instance of the declared type in one of the class interfaces. Another interface can stay the same as long as it includes declaration of the member variable's type interface.
course.h:
student.h:
你不能这样做,你必须将它们中的至少一个转换为指针。
You can't do that, you have to convert a least one of them to a pointer.