0

重複の可能性:
前方宣言をいつ使用するか?

Kubuntuとeclipseを使用しています。

私はそれらの2つのクラスを持っています:

Student.h

#ifndef STUDENT_H_
#define STUDENT_H_

#include <iostream>
#include <string>
#include <vector>

#include "Course.h"

using namespace std;

class Student {
    public:
        Student(vector<string> &vec);
        virtual void study(Course &c)=0;
        virtual ~Student();
    private:
        string _studentId;
        string _department;
        string _pathToImage;
};

#endif /* STUDENT_H_ */

Course.h

#ifndef COURSE_H_
#define COURSE_H_

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>

#include "Student.h"

using namespace std;
class Course {
    public:
        Course(vector<string> &vec);
        virtual void teach();
        virtual void reg(Student &s)=0;
        virtual ~Course();
    private:
        string _department;
        string _name;
        int _semester;
        int _minimumGrade;
};

#endif /* COURSE_H_ */

そして、準拠中にこのエラーが発生します:

Description Resource    Path    Location    Type
‘Course’ has not been declared  Student.h   /hw2/include    line 22 C/C++ Problem

それらのクラスを継承するクラスがさらにいくつかあります。これが問題だとは思いません。

何が問題になる可能性がありますか?

ありがとう

編集:

あなたの答えを記録するいくつかの編集の後、これは今私のファイルです:

Course.h

#ifndef COURSE_H_
#define COURSE_H_

#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <stdlib.h>

class Student;

using namespace std;
class Course {
    public:
        Course(vector<string> &vec);
        virtual void teach();
        virtual void reg(Student &s)=0;
        virtual ~Course();
    private:
        string _department;
        string _name;
        int _semester;
        int _minimumGrade;
};

#endif /* COURSE_H_ */

Student.h

#ifndef STUDENT_H_
#define STUDENT_H_

#include <iostream>
#include <string>
#include <vector>

class Course;


using namespace std;

class Student {
    public:
        Student(vector<string> &vec);
        virtual void study(Course &c)=0;
        virtual ~Student();
    private:
        string _studentId;
        string _department;
        string _pathToImage;
};

#endif /* STUDENT_H_ */

Course.cpp

#include "../include/Course.h"
#include "../include/Student.h"

Course::Course(vector<string> &vec): _department(""), _name(""), _semester(0), _minimumGrade(0) {
    _department = vec[0];
    _name = vec[1];
    _semester = atoi(vec[2].c_str());
    _minimumGrade = atoi(vec[3].c_str());
}

Course::~Course() {
    // TODO Auto-generated destructor stub
}

Student.cpp

#include "../include/Course.h"
#include "../include/Student.h"

Student::Student(vector<string> &vec): _studentId(""), _department(""), _pathToImage("") {
    _studentId = vec[0];
    _department = vec[1];
    _pathToImage = vec[2];
}
Student::~Student() {
    // TODO Auto-generated destructor stub
}

そして今、それは私にこのような多くのエラーを与えました:

Description Resource    Path    Location    Type
  required from ‘static void __gnu_cxx::__alloc_traits<_Alloc>::construct(_Alloc&, __gnu_cxx::__alloc_traits<_Alloc>::pointer, const _Tp&) [with _Tp = Student; _Alloc = std::allocator<Student>; __gnu_cxx::__alloc_traits<_Alloc>::pointer = Student*]’   hw2     line 202, external location: /usr/include/c++/4.7/ext/alloc_traits.h    C/C++ Problem
  required from ‘static void __gnu_cxx::__alloc_traits<_Alloc>::construct(_Alloc&, __gnu_cxx::__alloc_traits<_Alloc>::pointer, const _Tp&) [with _Tp = Course; _Alloc = std::allocator<Course>; __gnu_cxx::__alloc_traits<_Alloc>::pointer = Course*]’  hw2     line 202, external location: /usr/include/c++/4.7/ext/alloc_traits.h    C/C++ Problem
  required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Course; _Alloc = std::allocator<Course>; std::vector<_Tp, _Alloc>::value_type = Course]’   hw2     line 885, external location: /usr/include/c++/4.7/bits/stl_vector.h C/C++ Problem
  required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Course; _Alloc = std::allocator<Course>; std::vector<_Tp, _Alloc>::value_type = Course]’   hw2     line 893, external location: /usr/include/c++/4.7/bits/stl_vector.h C/C++ Problem
  required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::value_type = Student]’    hw2     line 885, external location: /usr/include/c++/4.7/bits/stl_vector.h C/C++ Problem
  required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Student; _Alloc = std::allocator<Student>; std::vector<_Tp, _Alloc>::value_type = Student]’    hw2     line 893, external location: /usr/include/c++/4.7/bits/stl_vector.h C/C++ Problem
4

2 に答える 2

3

インクルードを削除し、代わりにstudent.hclass Course;class Student;course.hに配置します。型は関数引数でのみ必要なので(実際にはそれらへの参照のみが必要なので)、型が不完全であるだけで十分です。

ここで、関連するインクルードを.cppファイルに追加する必要があります。たとえば、student.cppはとで始まる必要が#include "student.h"あり#include "course.h"ます。

于 2012-11-13T20:37:12.487 に答える
3

ここにあるものは循環依存関係と呼ばれます(Student.hはCourse.hに依存し、Student.hは...に依存します)。

これを回避するには、各ヘッダーファイルで使用するクラスを前方宣言する必要があります。

たとえば、ではStudent.h、をに置き換える必要が#include "Course.h"ありますclass Course。次に、実装ファイルにファイルを含めCourse.hてオブジェクトを使用できCourseます。

于 2012-11-13T20:39:51.357 に答える