请考虑以下代码片段:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
public class TestApplet extends JApplet
{
@Override
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
createGUI();
}
});
}
catch(InterruptedException | InvocationTargetException ex)
{
}
}
private void createGUI()
{
getContentPane().setLayout(new FlowLayout());
JButton startButton = new JButton("Do work");
startButton.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
JLabel label = new JLabel();
new Worker(label).execute();
}
});
getContentPane().add(startButton);
}
private class Worker extends SwingWorker<Void, …Run Code Online (Sandbox Code Playgroud) 请考虑以下三个简化文件:
student.h:
#ifndef STUDENT_H
#define STUDENT_H
#include "course.h"
class Student
{
private:
Course someCourse;
};
#endif
Run Code Online (Sandbox Code Playgroud)
course.h:
#ifndef COURSE_H
#define COURSE_H
#include "student.h"
class Course
{
private:
Student someStudent;
};
#endif
Run Code Online (Sandbox Code Playgroud)
和main.cpp:
#include "student.h"
int main();
Run Code Online (Sandbox Code Playgroud)
这不会编译给我
错误C2146:语法错误:缺少';' 在标识符'someStudent'之前
它会在更复杂的程序中产生更多错误(即使对于正确的代码部分).我猜设计是错误的:Student包含Course和Course包含Student.我想用它代表的是一个学生需要几门课程而一门课程有几个学生(我在一个完整的程序中使用向量,为简单起见,这里避免使用它们).有什么建议怎么可能?
弗拉德,提前谢谢.
更新:
感谢您的快速回复.Student课堂上的Course课堂前向声明(以及删除#include "student.h")似乎可以胜任.对不起,我觉得这里没关系,但实际上我在每个都使用const指针的向量(因为学生不应该能够控制a Course而a Course应该不能控制a Student),如:
vector<const Student* const> students; // in Course class
Run Code Online (Sandbox Code Playgroud)