[問題] include Student.h 與 class Student; 的差別
前幾天,我們老師出了一題作業,一些將 .cpp 和 .h 分開的簡單練習。
.h 即是宣告, .cpp 自然是實做內容了。
就是幾個 class 在那 include 來 include 去這樣。
先說一下,我的 compiler 是 gcc。
然後我遇到了個問題,話不多說,post code 先。
#ifndef _DEPARTMENT_H_
#define _DEPARTMENT_H_
#include <string>
#include <vector>
#include "CourseOffering.h"
class Student;
using namespace std;
class Department {
public:
Department(string);
string getName();
void addStudent(Student *);
Student* getStudent();
int numberOfStudents();
int numberOfCourses();
void DataOffering(CourseOffering *);
private:
string _name;
vector<Student *> _students;
vector<CourseOffering *> _offer;;
};
#endif
這是其中一個 .h 檔。我的問題即是黃色部份。
到底為什麼要寫 class Student; 呢?寫 include "Student.h" 不行嗎?
Student.h 裡確確實實只有 Student 的 class 宣告。
照理來說這兩種寫法意思應該一樣啊,但要是改成 include "Student.h"就一堆錯誤...
請問到底是差在哪呢?還有什麼情況要寫這種,什麼時候甫可寫 include "Student.h"?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.124.135.189
推
11/30 22:08, , 1F
11/30 22:08, 1F
→
11/30 22:17, , 2F
11/30 22:17, 2F
→
11/30 22:18, , 3F
11/30 22:18, 3F
→
11/30 22:49, , 4F
11/30 22:49, 4F
推
11/30 23:01, , 5F
11/30 23:01, 5F
→
11/30 23:04, , 6F
11/30 23:04, 6F
→
11/30 23:04, , 7F
11/30 23:04, 7F
→
11/30 23:05, , 8F
11/30 23:05, 8F
→
11/30 23:05, , 9F
11/30 23:05, 9F
→
11/30 23:10, , 10F
11/30 23:10, 10F
→
11/30 23:10, , 11F
11/30 23:10, 11F
→
11/30 23:10, , 12F
11/30 23:10, 12F
→
11/30 23:11, , 13F
11/30 23:11, 13F
→
11/30 23:11, , 14F
11/30 23:11, 14F
→
11/30 23:11, , 15F
11/30 23:11, 15F