【问题标题】:How to do mutual classes in c++ [duplicate]如何在c ++中做相互类[重复]
【发布时间】:2012-11-02 07:54:25
【问题描述】:

可能重复:
When to use forward declaration?

我正在使用 Kubuntu 和 eclipse。

我有这两个类:

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

我还有一些继承这些类的类。我不认为这是问题所在。

可能是什么问题?

谢谢

编辑:

在对您的答案进行一些编辑后,这是我现在的文件:

课程.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_ */

学生.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_ */

课程.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

【问题讨论】:

  • 如果你需要这些,你应该使用cstdlibcstdio而不是stdlib.hstdio.h
  • 您给出的错误不完整,请给出错误流的第一行。

标签: c++ class


【解决方案1】:

摆脱包含,而是将class Course; 放入student.h 并将class Student; 放入course.h。类型不完整就足够了,因为您只需要在函数参数中使用它们(实际上您只需要对它们的引用)。

您现在需要将相关的包含添加到 .cpp 文件中,例如student.cpp 应该以#include "student.h"#include "course.h" 开头。

【讨论】:

  • t 给了我很多错误,例如: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 = Course; _Alloc = std::allocator; __gnu_cxx::__alloc_traits<_alloc>::pointer = Course*]’ hw2 line 202,外部位置:/usr/include/c++/4.7/ext/alloc_traits.h C/C++问题
  • 不要删除 所有 包含的内容,只删除您自己的两个标题。
  • 我完全按照你说的做了。我将#include "Course.h" 替换为课程课程;和 #include "Student.h" 与学生班级;
  • @Nir:啊,我忘记了,您仍然需要将完整的包含放入 .cpp 文件中。让我修改一下。
  • 我编辑了我的问题,我添加了我编辑的文件,包括 cpp 文件。请看一下。
【解决方案2】:

您在这里拥有的称为循环依赖(Student.h 依赖于 Course.h,而 Course.h 又依赖于 Student.h,而后者又依赖于...)。

要解决这个问题,您应该转发声明您在每个头文件中使用的类。

例如,在Student.h 中,您应该将#include "Course.h" 替换为class Course。然后在实现文件中包含Course.h 文件并使用Course 对象。

【讨论】:

  • 它给了我很多错误,例如: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 = Course; _Alloc = std::allocator; __gnu_cxx::__alloc_traits<_alloc>::pointer = Course*]' hw2 line 202,外部位置:/usr/include/c++/4.7/ext/alloc_traits.h C/C++问题
猜你喜欢
  • 2013-09-26
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 2011-02-02
相关资源
最近更新 更多