0

Possible Duplicate:
When to use forward declaration?

I'm using Kubuntu & eclipse.

I have those two classes:

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_ */

And i'm getting this error while compliing:

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

I have some more classes that inheriting those class. I don't think that this is the problem.

What can be the problem?

Thanks

EDIT:

After some edits recording to your answers, this is my files now:

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
}

and it's now given me alot of error like this:

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
Community
  • 1
  • 1
Nir
  • 1,882
  • 4
  • 26
  • 44

2 Answers2

3

Get rid of the includes, and instead just put class Course; into student.h and class Student; into course.h. It's sufficient for the types to be incomplete, since you only need them in function arguments (and actually you only need references to them).

You will now need to add the relevant includes to the .cpp files, e.g. student.cpp should start with #include "student.h" and #include "course.h".

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • t's given me alot of errors like : 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, external location: /usr/include/c++/4.7/ext/alloc_traits.h C/C++ Problem – Nir Nov 13 '12 at 20:43
  • Don't get rid of *all* the includes, just the two of your own headers. – Kerrek SB Nov 13 '12 at 20:44
  • I did exactly what you said. I replace #include "Course.h" with class Course; and #include "Student.h" with class Student; – Nir Nov 13 '12 at 20:47
  • @Nir: Ah, I forgot, you still need to put the full includes into the .cpp files. Let me amend that. – Kerrek SB Nov 13 '12 at 20:49
  • I edited my question, I added my edited files, including cpp files. please take a look. – Nir Nov 13 '12 at 20:55
  • It's hard to tell, because your code does *not* show precisely the two functions that would have been relevant to the problem :-( Try narrowing it down. (Spend at least an hour on your own on this, though!) – Kerrek SB Nov 13 '12 at 20:57
  • But I even don't understand the problem... I did exactly what you said. What else it can be? – Nir Nov 13 '12 at 21:00
  • @Nir: Start by removing the declarations of the member functions `study` and `reg` and start narrowing it down. – Kerrek SB Nov 13 '12 at 21:45
3

What you have here is called a circular dependency (Student.h depends on Course.h which depends on Student.h which depends on ...).

To get around this you should forward declare the classes you use in each header file.

For example, in Student.h, you should replace #include "Course.h" with a class Course. Then in the implementation file you can include the Course.h file and use the Course object.

Kyle Lutz
  • 7,966
  • 2
  • 20
  • 23
  • It's given me alot of errors like : 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, external location: /usr/include/c++/4.7/ext/alloc_traits.h C/C++ Problem – Nir Nov 13 '12 at 20:43