roi hoyli
roi hoyli

Reputation: 23

Class pointer to different .h file

Helo stack people, I need your help in work I make. So I have to .h files first one is Course and the second is Student and I try to create function call getCourses but Unfortunately It's not going so well.My realization "Course ** courses" do not pass compiling and I do not know why not. I would appraise if you can help me to Understand my mistake and help me to fix them thanks.

getCourse - return list of courses

my Course .h file

#ifndef _CORSE_H
#define _CORSE_H
#include <iostream>
#include "Student.h"

class Course
{
public:
void init(std::string getName, int test1, int test2, int exam);
std::string getName();
int* getGrades();
double getFinalGrade();

private:
std::string _name;
int _exam;
int _test1;
int _test2;
};

#endif

My Student .h file-

#ifndef _STUDENT_H
#define _STUDENT_H
#include <iostream>
#include "Course.h"

class Student
{
public:
void init(std::string name, Course** courses, int crsCount);
std::string getName();
void setName(std::string name);
double getAvg();
int getCrsCount();
Course** getCourses();

private:
std::string _name;
Course** courses;
int _crsCount;
};


#endif

My get course function -

Course** student::getCourses()
{
  return(this->courses);
}

The problem in the "Course** getCourses();"initialization and this applies also to the init function and Course** getCourses(); function. error C4430: missing type specifier - int assumed. Note:C++ doe not support default-int

Upvotes: 1

Views: 91

Answers (2)

A.J.
A.J.

Reputation: 365

Besides what Mike Seymour wrote your course function uses lowercase student in
Course** student::getCourses()

Whereas you declared the class with a capital S. Case does matter.

Upvotes: 0

Mike Seymour
Mike Seymour

Reputation: 254651

You have a circular dependency - each header tries to include the other, and you end up with one class defined before the other. This gives errors, because you have to declare a type before you can use its name.

Course doesn't depend on Student at all, so just remove the #include from that file.

The definition of Student only uses pointers to Course so it doesn't need the full definition. It only needs to know that the class exists, so you can replace the #include with a declaration:

class Course;

A couple more points:

  • both headers should include <string> since they use std::string; but not <iostream> since they don't use any I/O streams;
  • names beginning with an underscore and a capital, like _CORSE_H, are reserved. You should remove the underscores.
  • you've mis-capitalised Student in the final code snippet.

Upvotes: 4

Related Questions